Reading in strings -> numbers ??
Johann Hibschman
johann at physics.berkeley.edu
Mon May 1 23:38:57 EDT 2000
Louis M Pecora 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.
Well, I've been doing numerics in python for years now, which is more
than a little scary, and it's never been a problem for me.
I tend to either use Konrad Hinsen's ArrayIO module, which will semi-
automatically do this for me, or simply do a
lines = open(filename).readlines()
lines = map(string.split, lines)
lines = map(lambda l: map(float, l), lines)
data = array(lines)
and I'm set. Usually I write code like this once for any particular
data format, then simply call it when I need it.
Whitespace-delimited files are very easy to parse with string.split.
The column-oriented stuff you get from Fortran format statements is a
bit harder, but it's easy enough to read in lines, take substrings,
then evaluate those substrings as you need them.
--Johann
--
Johann Hibschman johann at physics.berkeley.edu
More information about the Python-list
mailing list