Reading in strings -> numbers ??

Louis M. Pecora pecora at anvil.nrl.navy.mil
Tue May 2 07:26:01 EDT 2000


[[ This message was both posted and mailed: see
   the "To," "Cc," and "Newsgroups" headers for details. ]]

In article <uvh0x20iy.fsf at ctwd0143.fitlinxx.com>, David Bolen
<db3l at fitlinxx.com> wrote:

> "Louis M. Pecora" <pecora at anvil.nrl.navy.mil> writes:
> 
> > 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.
> 
> I definitely don't think handling a file like this is a "work-around",
> but that's also because I don't think it requires a direct correlation
> to the [f]scanf function.  Assuming for the moment that your lines are
> columns of integers, this would be one way to process the file:
> 
>         import string
> 
>         input = open('filename')
>         while 1:
>             line = input.readline()
>             if not line: break
>             columns = map(int,string.split(line))

David, Thanks for the nice pointer/tutorial.  I will look it over to
really understand how to vary the approach.  Thanks, again.



More information about the Python-list mailing list