I am writing Python code that takes in a .csv file and uses the data in it to create graphs. I am using numpy.loadtxt to accomplish this. The beginning of my code is as follows:
import numpy
infile = 'dicomfile-test.csv'
print(sys.argv)
if len(sys.argv) > 1:
infile = sys.argv[1]
xprompts, xrandoms, xduration, xheight, xweight, xdose, xuptime, xsf, xdtcf =\
numpy.loadtxt(infile, delimiter=',',\
dtype = 'float,float,float,float,float,float,float,float,float',\
usecols = (0,1,2,3,4,5,6,7,8), unpack = True, skiprows = [1])
The variables labeled "xprompts", "xrandoms", etc. are the names for each column in the .csv file. Each column has 9 rows of numbers that the code is supposed to run on. My error that is coming up is:
Traceback (most recent call last):
File "/Users/mycomputer/Desktop/Project/test-curve.py", line 28, in <module>
np.loadtxt(infile, delimiter=',',\
File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/numpy/lib/npyio.py", line 1338, in loadtxt
arr = _read(fname, dtype=dtype, comment=comment, delimiter=delimiter,
File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/numpy/lib/npyio.py", line 962, in _read
_check_nonneg_int(skiplines)
File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/numpy/lib/npyio.py", line 774, in _check_nonneg_int
raise TypeError(f"{name} must be an integer") from None
TypeError: argument must be an integer
I am fairly new to Python, so I am having trouble understanding what to change in my code to fix this error. The TypeError says the argument must be an integer, which is where my confusion is since everything in my .csv file is an integer. Any help would be appreciated – thank you!
This is not related to the data but to the argument skiprows
, which must be an int
instead of a list
. If you want to skip the first row, do skiprows=1
instead of skiprows=[1]
.
From the doc:
skiprows : int, optional
Skip the first `skiprows` lines, including comments; default: 0.