scanf in python...?

bvadavalli at my-deja.com bvadavalli at my-deja.com
Wed Nov 15 10:24:16 EST 2000


In article <KPRG5.14562
$f5.508728 at news1.sttls1.wa.home.com>,
  "Greg Jorgensen" <gregj at pobox.com> wrote:
> "Tony Waterman" <tonyw54 at home.com> wrote in
message
> news:39ebd839.159714 at news...
> > Does python have any way to mimic scanf ? I
just bought Python on
> > Win32 and I can't find anything in there
about it, or on the
> > python.org html tutorial. Also; how many ways
to mimic scanf are
> > there?
>
> scanf is necessary in C because C is a
statically-typed language. Python is
> dynamically typed--names can represent any type
and change type at runtime.
>
> You can use Pythons string functions and
conversions to accomplish what you
> would do with scanf in C:
>
> # assume input lines: lastname firstname salary
> total = 0.0
> while 1:
>     s = sys.stdin.readline()
>     if not s:
>         break
>     t = s.split()    # break s on whitespace
into list of three elements
>     total += float(t[2])    # convert salary to
float, add to total
>     ...
>
> This technique will work with simple delimited
input lines. You can handle
> almost anything with regular expressions.
>
> --
> Greg Jorgensen
> Deschooling Society
> Portland, Oregon, USA
> gregj at pobox.com
>
>

I am sorry to jump in but I was having a similar
problem and thought you could help. When I
couldnt get to read a line of input from stdin
with readline, I tried your code segment and I
still have the same problem. It behaves as though
there is no loop and does print anything or read
anything. It just does nothing!
Code:
import sys
while 1:
    import sys
    s = sys.stdin.readline()
    if not s:
        break
    t = s.split()
    print t


Sent via Deja.com http://www.deja.com/
Before you buy.



More information about the Python-list mailing list