[Numpy-discussion] read ascii file with quote delimited strings
Thu Feb 25 14:30:33 CST 2010
Sam Tygier wrote:
> I am trying to read an ascii file which mixes ints, floats and stings.
> 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.
Does each column always contain the same number of characters? That is,
are the field widths always the same? If so, you can give the
'delimiter' argument of numpy.genfromtxt a list of field widths. (This
is true even in numpy 1.3.0, though it does not appear to be documented.)
An example is attached to this post to scipy-user:
> I have tried giving loadtxt a dtype that specifies the length of the
> [('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
> NumPy-Discussion mailing list
More information about the NumPy-Discussion