high-level "read" function?

Michael Muller proteus at cloud9.net
Tue Jun 29 17:56:35 CEST 1999

Christine Celata wrote:
> I am just starting to use Python.  It is foreign to me-- I'm a physicist
> used to Fortran, not C.  I have succeeded in getting Python to read
> program user input, but as far as I can see, in order to get it to read
> numbers separated by commas from the terminal and store them as floating
> point variables, I have to use a sequence of low-level call to:  read
> the input as one character string, then strip off the carriage return,
> then split the string into several strings using the commas as markers
> for the end of the string, then convert the separated strings into
> floating point numbers.  In the case where I am reading in strings
> separated by commas the last step is omitted, but I have to add a step
> to eliminate leading whitespace.  I am about to try to write a function
> for myself that does all this, and a function to read integers, and a
> function to read lists of strings, but--  Since lots of people must be
> inputting numbers, isn't this stuff already written and lying around
> somewhere?  I can't find it in the documentation for modules.   Thanks.

You want to look at the string and regular expression modules.  This
should pretty much do what you want:

 >>> import re
 >>> line = raw_input()
 >>> map(float, re.split('\s*,\s*', line))

michaelMuller = proteus at cloud9.net | http://www.cloud9.net/~proteus
Government, like dress, is the badge of lost innocence; the palaces of
are built on the ruins of the bowers of paradise. - Thomas Paine

More information about the Python-list mailing list