With a text list:
In [338]: txt = '''1, 1.3, abcde
...: 2, 1.3, def'''.splitlines()
The structured array:
In [339]: np.genfromtxt(txt, dtype=None, delimiter=',', encoding=None)
Out[339]:
array([(1, 1.3, ' abcde'), (2, 1.3, ' def')],
dtype=[('f0', '<i8'), ('f1', '<f8'), ('f2', '<U6')])
Trying to specify object - each item its own type:
In [340]: np.genfromtxt(txt, dtype=object, delimiter=',', encoding=None)
Out[340]:
array([[b'1', b' 1.3', b' abcde'],
[b'2', b' 1.3', b' def']], dtype=object)
It doesn't try to convert any strings to numbers.
converters
converts columns right, but for some reason still makes a structured array:
In [341]: np.genfromtxt(txt, dtype=object, delimiter=',', encoding=None, convert
...: ers={0:int, 1:float})
Out[341]:
array([(1, 1.3, b' abcde'), (2, 1.3, b' def')],
dtype=[('f0', '<i8'), ('f1', '<f8'), ('f2', 'O')])
But you could convert the structured array to object dtype via a list:
In [346]: np.genfromtxt(txt, dtype=None, delimiter=',', encoding=None)
Out[346]:
array([(1, 1.3, ' abcde'), (2, 1.3, ' def')],
dtype=[('f0', '<i8'), ('f1', '<f8'), ('f2', '<U6')])
In [347]: np.array(_.tolist(), object)
Out[347]:
array([[1, 1.3, ' abcde'],
[2, 1.3, ' def']], dtype=object)
Another option is to split the lines yourself, building a list of lists. genfromtxt
is doing that with few more bells and whistles.
In [357]: lines=[]
...: for line in txt:
...: i = line.split(',')
...: x = (int(i[0]), float(i[1]), i[2].strip())
...: lines.append(x)
In [358]: lines
Out[358]: [(1, 1.3, 'abcde'), (2, 1.3, 'def')]
In [359]: np.array(lines,object)
Out[359]:
array([[1, 1.3, 'abcde'],
[2, 1.3, 'def']], dtype=object)
But beware that you can't do math on that object array as well as on a numeric array, or even the numeric fields of the structured array.