0

I'm trying to use np.genfromtxt to read in a text file into a variety of data types, some of which are ideally sub arrays, however I can't seem to find the right code or input format for this, I've managed to get numpy to complain I both have too many and too few columns!

example data:

id;bar;foo;snakes
1;2.46;2.616;(1,1)

(or without brackets, e.g. 1;2.46;2.616;1,1 )

x = np.genfromtxt(file, delimiter=';', names=True, dtype='u4,f8,f8,(2,)u4')

Results in "ValueError: could not assign tuple of length 4 to structure with 5 fields."

id;bar;foo;snakes
1;2.46;2.616;1;1

Results in "ValueError: Some errors were detected ! Line #2 (got 5 columns instead of 4)"

Tried different delimiters. Is this not possible? The two different errors suggests it should be but I'm just not getting the correct syntax or data format

hpaulj
  • 221,503
  • 14
  • 230
  • 353
James B
  • 1
  • 1
  • Even though the `dtype` has nesting, the `csv` is loaded as 'flat' rows. Your 2nd example will work if the `names` row has 5 columns. In other words, the file has to be a valid 2d csv with `dtype=None`. The users defined dtype is just a refinement on what it would deduce by default. – hpaulj Dec 08 '22 at 17:47
  • `genfromtxt` first splits on the delimiter. Then it tries to allocate the values to the dtype fields. In the first case it gets 4 values per row. The last is a string "(1,1)". It doesn't try to split that on a sub-delimiter. Your dtype, flattened, has 5 fields; it requires 5 values. – hpaulj Dec 08 '22 at 18:10
  • Thanks, this explains it nicely. Have it working now. – James B Dec 12 '22 at 10:52

0 Answers0