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

Warren Weckesser warren.weckesser@enthought....
Thu Feb 25 14:30:33 CST 2010


Sam Tygier wrote:
> 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.
>   

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:
    http://mail.scipy.org/pipermail/scipy-user/2010-February/024333.html


Warren

> 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
>
>
>
>
>
>
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion@scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>   



More information about the NumPy-Discussion mailing list