Reading in strings -> numbers ??

Louis M. Pecora pecora at anvil.nrl.navy.mil
Sun Apr 30 10:22:35 EDT 2000


In article <8ede36$9r6$1 at nnrp1.deja.com>, <chris_barker at my-deja.com>
wrote:

> Well, the partner to file.write is file.read. What you are really
> looking for is a partner to '%f %f'%(3,4), which is essentially an
> sprintf(). So, what you want is an sscanf() or fscanf(). I've been
> looking for that for a while. What I would like is a version like the
> one in MATLAB, that gives you a whole matrix in one swipe.
> 
> Unfortunatly, it doesn't exist for python. Someone did make a Python
> sscanf module, but I never got around to compiling it. there is also a
> tablio (or something like that) module around. In the mean time, you can
> always use something like:
> 
> 
> list = map(float,string.split(file.readline)))
> 
> or, to use your example:
> 
> >>> map(float,string.split((" %e %e %d" % (1.2, -3.5, 10))))
> [1.2, -3.5, 10.0]
> 
> it doesn't work if you want mixed types, but it gets you by.

Chris,

Thanks. I'll try that.  

After three weeks of learning Python have I actually found a real wart? 
A common requirment in programming (especially for numerical stuff) is
to read in data that is often generated by other programs and other
people.  The common form is a "table" structure:

data11(white space)data12(white space)...data1m(return/newline)
data21(white space)data22(white space)...data2m(return/newline)
...
datan1(white space)datan2(white space)...datanm(return/newline/EOF)
EOF

So you're saying that reading in something as basic as this is a
"work-around?"  Sigh.

Thanks for your help.



More information about the Python-list mailing list