sys.stdin.readline 
Author Message
 sys.stdin.readline

The code segment below gives me nothing as though the loop didnt exist.
Please help!
while 1:
     s = sys.stdin.readline()
     if not s:
         break
     t = s.split()
print t

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



Sun, 04 May 2003 03:00:00 GMT  
 sys.stdin.readline

Quote:
>The code segment below gives me nothing as though the loop didnt exist.
>Please help!
>while 1:
>     s = sys.stdin.readline()
>     if not s:
>         break
>     t = s.split()
>print t

It does work, but it only gives you the result of splitting the last line.
If you have a blank line at the end of the file t will be an empty list.

Perhaps you meant to collect the input from all of the lines. If so
initialise t outside the top of the loop "t = []" and append to it inside
the loop: "t.append(s.split())" or "t.extend(s.split())" depending on what
you wanted.



Sun, 04 May 2003 03:00:00 GMT  
 sys.stdin.readline

Quote:

>The code segment below gives me nothing as though the loop didnt exist.
>Please help!
>while 1:
>     s = sys.stdin.readline()
>     if not s:
>         break
>     t = s.split()
>print t

Have you tried putting the print inside the loop?

while 1:
  s = sys.stdin.readline()
  if not s:
    break
  t = s.split()
  print t

--
Grant Edwards                   grante             Yow!  I'm GLAD I
                                  at               remembered to XEROX all
                               visi.com            my UNDERSHIRTS!!



Sun, 04 May 2003 03:00:00 GMT  
 sys.stdin.readline

Quote:

> The code segment below gives me nothing as though the loop didnt
exist.
> Please help!
> while 1:
>      s = sys.stdin.readline()
>      if not s:
>          break
>      t = s.split()
> print t

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

There were several problems in this code. I thank all those who tried to
help.
1. I used windows copy paste which lost the tabs.
2. I should have imported string module
3. The break condition should be true
4. I miss used split

The code that works for me:
import sys,string
s=[]
while 1:
        s = sys.stdin.readline()
        if  s:
                break
print s
s = string.split(s)
print s

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



Sun, 04 May 2003 03:00:00 GMT  
 sys.stdin.readline

Quote:

> The code segment below gives me nothing as though the loop didnt exist.
> Please help!

Have you tried stepping through it in the pdb de{*filter*}?
Or even inserting print statements at some critical points...
after the assignments might be smart, thus:

while 1:
      s = sys.stdin.readline()
      print len(s)  # len in case its not ASCII...
      if not s:
          break
      t = s.split()
      print len(t)
 print t

What happens now?

Alan g.
--
=================================================
This post represents the views of the author
and does not necessarily accurately represent
the views of BT.



Mon, 12 May 2003 03:00:00 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. using sys.stdin.raw_input and sys.stdin.readline together in a platform independent way

2. sys.stdin.readlines() in 2.2a ?

3. Emacs EOF on sys.stdin.readline()

4. Chosing between sys.argv and sys.stdin

5. Readline loses stdin?

6. non-blocking sys.stdin.read() on win32?

7. banging head against sys.stdin and tkinter on win32

8. sys.stdin.read() HELP!

9. reusing sys.stdin?

10. how to calculate the size of sys.stdin?

11. Error using sys.stdin.read()

12. can i make sys.stdin.read() not wait?

 

 
Powered by phpBB® Forum Software