[Numpy-discussion] read ascii file with quote delimited strings

Sam Tygier Sam.Tygier@hep.manchester.ac...
Thu Feb 25 11:10:32 CST 2010


Hi

I am trying to read an ascii file which mixes ints, floats and stings.
eg.
1 2.3 'a' 'abc  '
2 3.2 'b' '     '
3 3.4 ' ' 'hello'

Within a column that data is always the same. the strings are sometimes
contain with spaces.

I have tried giving loadtxt a dtype that specifies the length of the
strings:
[('a', int), ('b', float), ("c", "a1"), ("d", "a5")]
or including the quotes:
[('a', int), ('b', float), ("c", "a3"), ("d", "a7")]

but it seems that loadtxt uses split() before looking at the dtype, so
for example line 3 becomes
["3", "3.4", "'", "'", "'hello'"]
and my 2 string elements contain only quotes.

Would it be possible a dtype of "a3" to force reading 3 chars?

Or would it make more sense for loadtxt to have a quote_char, that i
could set to "'". This would make it ignore whitespace between
quote_chars.

Sam








More information about the NumPy-Discussion mailing list