<br><br><div class="gmail_quote">On Fri, Feb 26, 2010 at 4:29 PM, Warren Weckesser <span dir="ltr"><<a href="mailto:warren.weckesser@enthought.com">warren.weckesser@enthought.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<div><div></div><div class="h5">Ralf Gommers wrote:<br>
> Hi all,<br>
><br>
> I'm trying to read in data from text files with genfromtxt, and have<br>
> some trouble figuring out the right combination of keywords. The<br>
> format is:<br>
><br>
> ['0\t\t4.000000000000000e+007,0.000000000000000e+000\n',<br>
>  '\t9.860280631554179e-001,-1.902586503306264e-002\n',<br>
>  '\t9.860280631554179e-001,-1.902586503306264e-002']<br>
><br>
> Note that there are two delimiters, tab and comma. Also, the first<br>
> line has an extra integer plus tab (this is a repeating pattern). <br>
><br>
<br>
</div></div>The 'delimiter' keyword does not accept a list of strings.  If it is a<br>
list, it must be a list of integers that are the field widths.  In your<br>
case, that won't work.<br>
<br>
You could try fromregex:<br>
<br>
-----<br>
In [1]: import numpy as np<br>
<br>
In [2]: cat sample.raw<br>
0        4.000e+007,0.00000e+000<br>
    9.8602806e-001,-1.9025e-002<br>
    9.8602806e-001,-1.9025e-002<br>
123        5.0e6,100.0<br>
    10.1,-2.0e-3<br>
    10.2,-2.1e-3<br>
<br>
<br>
In [3]: a = np.fromregex('sample.raw', '(.*?)\t+(.*),(.*)',<br>
np.dtype([('extra', 'S8'), ('x', float), ('y', float)]))<br>
<br>
In [4]: a<br>
Out[4]:<br>
array([('0', 40000000.0, 0.0), ('', 0.98602805999999998, -0.019025),<br>
       ('', 0.98602805999999998, -0.019025), ('123', 5000000.0, 100.0),<br>
       ('', 10.1, -0.002), ('', 10.199999999999999,<br>
-0.0020999999999999999)],<br>
      dtype=[('extra', '|S8'), ('x', '<f8'), ('y', '<f8')])<br>
<br>
<br>
Note that the first field of the array is a string, not an integer.  The<br>
string will be empty in rows that did not have the initial integer.  I<br>
don't know if that will work for you.<br>
<br></blockquote><div>That works, thanks. I had hoped that genfromtxt could do it because it can skip the header and is presumably faster. But I'll take what I can get.<br><br>Cheers,<br>Ralf</div></div><br>