Mailing List Archive

high-level "read" function?
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.
high-level "read" function? [ In reply to ]
On Mon, 28 Jun 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,

input() can do most of the job for you:

>>> userinput = input("Enter a bunch of number: ")
Enter a bunch of number: 1.24, 400, 15.5
>>> print userinput
(1.24, 400, 15.5)
>>> type(userinput)
<type 'tuple'>
>>> type(userinput[0])
<type 'float'>
>>> type(userinput[1])
<type 'int'>

If you really need them all to be floating point variables, you can do
that with:

>>> floats = map(float, userinput)
>>> print floats
[1.24, 400.0, 15.5]

--david ascher
high-level "read" function? [ In reply to ]
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@cloud9.net | http://www.cloud9.net/~proteus
-----------------------------------------------------------------------------
Government, like dress, is the badge of lost innocence; the palaces of
kings
are built on the ruins of the bowers of paradise. - Thomas Paine
=============================================================================