473,385 Members | 1,641 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

sys.stdin.readline()

When I invoke readline() in a for loop, why does it return a series of
one-char strings, rather than the full line?
for sL in sys.stdin.readline(): print sL

....
abc
a
b
c

(I typed in 'abc', and the loop printed out 'a\nb\nc\n')

I.e. how can I make readline() wait for the newline before returning a
value? 'readline()' seems to be acting exactly like 'read()' here.

('readlines()' works fine in this context, except that it waits for
eof; I'd really rather iterate over lines in stdin as they come in)
Jul 18 '05 #1
12 53216
On 2004-08-31, Mike Maxwell <ma*****@ldc.upenn.edu> wrote:
When I invoke readline() in a for loop, why does it return a series of
one-char strings, rather than the full line?
for sL in sys.stdin.readline(): print sL
...
abc
a
b
c

(I typed in 'abc', and the loop printed out 'a\nb\nc\n')

I.e. how can I make readline() wait for the newline before returning a
value?


It is.
'readline()' seems to be acting exactly like 'read()' here.


Sort of.

What you want is:

import sys
while True:
s = sys.stdin.readline()
if not s:
break
print s

--
Grant Edwards grante Yow! .. I see TOILET
at SEATS...
visi.com
Jul 18 '05 #2
Mike Maxwell wrote:
When I invoke readline() in a for loop, why does it return a series of
one-char strings, rather than the full line?
for sL in sys.stdin.readline(): print sL


It does return a full line. *One* line. Then your loop iterates
over the characters in that line.

Try `for sL in sys.stdin.xreadlines(): print sL'.
Or in newer Pythons, simply `for sL in sys.stdin: print sL'.

--
Hallvard
Jul 18 '05 #3
Hallvard B Furuseth <h.b.furuseth <at> usit.uio.no> writes:
Or in newer Pythons, simply `for sL in sys.stdin: print sL'.


This doesn't work for me in the interactive shell:

Python 2.4a2 (#55, Aug 5 2004, 11:42:43) [MSC v.1310 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
import sys
for l in sys.stdin:

.... print repr(l)
....
A
B
C
D
^Z
'A\n'
'B\n'
'C\n'
'D\n'

Note that I don't get output until I hit ^Z (this is, obviously, a Windows
box). I tried starting Python with the -u option too, and I still get this
result. Is there any way to make 'for l in sys.stdin' work the way you
suggest it does at an interactive prompt?

Steve

Jul 18 '05 #4
Steven Bethard wrote:
Hallvard B Furuseth <h.b.furuseth <at> usit.uio.no> writes:
Or in newer Pythons, simply `for sL in sys.stdin: print sL'.
This doesn't work for me in the interactive shell:
(...)


Whoops - you just found a mysterious bug in a program I'm writing.
Thanks:-)
Note that I don't get output until I hit ^Z (this is, obviously, a Windows
box). I tried starting Python with the -u option too, and I still get this
result. Is there any way to make 'for l in sys.stdin' work the way you
suggest it does at an interactive prompt?


Can't see any in the manual, now that I've looked more closely.
Info node "(python-lib)File Objects" says file.next() uses a read-
ahead buffer. It doesn't say one can disable that.

--
Hallvard
Jul 18 '05 #5
Hallvard B Furuseth wrote:
Mike Maxwell wrote:
When I invoke readline() in a for loop, why does it return a series of
one-char strings, rather than the full line?
>for sL in sys.stdin.readline(): print sL

It does return a full line. *One* line. Then your loop iterates
over the characters in that line.


LoL, thanks!
Try `for sL in sys.stdin.xreadlines(): print sL'.
Or in newer Pythons, simply `for sL in sys.stdin: print sL'.


I think I saw that, but when I tried it, I got:

/lib/python2.3/pydoc.py:250: DeprecationWarning:
xreadlines is deprecated; use 'for line in file'.

--which got me off on this dead end.

What I was originally trying to do, is to implement a one-liner that
would act something like 'sed', but applying to Unicode characters. An
example would have a command that looked something like
"s/u'\u0D0A'//"
i.e. delete all instances of the Unicode char U+0D0A (which you can't do
with 'sed', at least not the version that I'm using).

The guy down the hall does these kinds of things with perl one-liners,
but I have more dignity than to use perl... Unfortunately, it's looking
more and more complex to do one-liners like this in Python. Am I
overlooking s.t.?

Mike Maxwell
Jul 18 '05 #6
Mike Maxwell <ma*****@ldc.upenn.edu> wrote:
...
The guy down the hall does these kinds of things with perl one-liners,
but I have more dignity than to use perl... Unfortunately, it's looking
more and more complex to do one-liners like this in Python. Am I
overlooking s.t.?


No, I think you're correctly observing that Python isn't oriented to
one-liners -- not at all. Most interesting things in Python require
more than one line. You could write a "oneliners shell" that takes some
defined separator and turns it into a newline, of course, e.g.:

bangoneliner.py:
#!/usr/bin/env python
import sys
oneliner = sys.argv.pop(1)
exec '\n'.join(oneliner.split('!'))

now, sometying like

bangoneliner.py 'for x in xrange(7):! if x%2:! print x'

should work (note that inserting the spaces after the bangs to simulate
proper indentation IS a silly fuss, but you hafta...:-).
Alex
Jul 18 '05 #7
Alex Martelli wrote:
Mike Maxwell <ma*****@ldc.upenn.edu> wrote:
No, I think you're correctly observing that Python isn't oriented to
one-liners -- not at all. Most interesting things in Python require
more than one line.
<rant>
I don't care whether it's "interesting", I just want to get some work
done. And since most of the text processing tools in Unixes that I
would otherwise use (grep, sed, tr) don't support Unicode, and are
inconsistent in their regular expression notation to boot, it would be
nice if I could write regex operations in a single, consistent
programming language. Python is a single, consistent programming
language, but as you say, it doesn't lend itself to one-liners.
</rant>
You could write a "oneliners shell" that takes some
defined separator and turns it into a newline, of course, e.g.:

bangoneliner.py:
#!/usr/bin/env python
import sys
oneliner = sys.argv.pop(1)
exec '\n'.join(oneliner.split('!'))

now, sometying like

bangoneliner.py 'for x in xrange(7):! if x%2:! print x'

should work
Hmm, I may give that a try...thanks!
note that inserting the spaces after the bangs to simulate
proper indentation IS a silly fuss, but you hafta...:-).


Well, I guess I could translate some other char (one that's easier to
count than spaces) into indents, too.

Mike Maxwell
Jul 18 '05 #8
Mike Maxwell <ma*****@ldc.upenn.edu> wrote:
Alex Martelli wrote:
Mike Maxwell <ma*****@ldc.upenn.edu> wrote:
No, I think you're correctly observing that Python isn't oriented to
one-liners -- not at all. Most interesting things in Python require
more than one line.
<rant>
I don't care whether it's "interesting", I just want to get some work
done.


Something that lets you get work done IS thereby interesting. Most
interesting things in Python require more than one line. So, I don't
see the basis for your rant.
And since most of the text processing tools in Unixes that I
would otherwise use (grep, sed, tr) don't support Unicode, and are
inconsistent in their regular expression notation to boot, it would be
nice if I could write regex operations in a single, consistent
programming language. Python is a single, consistent programming
language, but as you say, it doesn't lend itself to one-liners.


No, but a supporting script similar to the one I suggest below can
easily be adapter to offer more sensible functionality than any oneliner
might sensibly support -- for example (for the kinds of tasks you imply)
by including and automatically using such modules as re and fileinput.
now, sometying like

bangoneliner.py 'for x in xrange(7):! if x%2:! print x'

should work


Hmm, I may give that a try...thanks!


You're welcome.

note that inserting the spaces after the bangs to simulate
proper indentation IS a silly fuss, but you hafta...:-).


Well, I guess I could translate some other char (one that's easier to
count than spaces) into indents, too.


Sure, or you could use (e.g.) '!3' to translate into 'newline then three
spaces', or use block start/endmarkers and translate them into
indents/dedents, etc, etc.

Personally, given your now-restated problem, that you need 'better'
versions of grep, sed and tr, I would take another tack -- I would
reimplement _those_ in Python with its re sublanguage and Unicode
support. Using them should be easier and tighter than putting newliners
together, I think.
Alex
Jul 18 '05 #9
Mike Maxwell wrote:
Alex Martelli wrote:
Mike Maxwell <ma*****@ldc.upenn.edu> wrote:
No, I think you're correctly observing that Python isn't oriented to
one-liners -- not at all. Most interesting things in Python require
more than one line.


<rant>
I don't care whether it's "interesting", I just want to get some work
done. And since most of the text processing tools in Unixes that I
would otherwise use (grep, sed, tr) don't support Unicode, and are
inconsistent in their regular expression notation to boot, it would be
nice if I could write regex operations in a single, consistent
programming language. Python is a single, consistent programming
language, but as you say, it doesn't lend itself to one-liners.
</rant>


Most of us who do this conclude that our potential one-liner
is actually likely to be re-used, and we write the Python
three-liner equivalent in a file. Later on, we typically find
that it has grown to seventeen lines and we don't particularly
mind the slight extra inconvenience of having to put the
initial code in a file instead of trying to retype it each
time we need it.

-Peter
Jul 18 '05 #10
I don't want to drag this out any longer. Thanks to all the posters.
Several mentioned that one-liners tend to both get re-used and to grow
to multiple lines, and that that implied that maybe the notion of a
one-liner was faulty. This may be true, and I may find myself doing
exactly what they suggest--building Unicode-aware versions of grep, sed,
and tr (hopefully not awk :-)). Time will tell.

Thank you also to Simon B. (si***@brunningonline.net) for pointing me to
his one-liner implementation, which is much better than I {w|c}ould have
done.

Mike Maxwell
Jul 18 '05 #11
I just realized...

Mike Maxwell wrote:
When I invoke readline() in a for loop, why does it return a series of
one-char strings, rather than the full line?
for sL in sys.stdin.readline(): print sL


for sL in iter(sys.stdin.readline, ''): print sL,

--
Hallvard
Jul 18 '05 #12
Hallvard B Furuseth wrote:
Mike Maxwell wrote:
When I invoke readline() in a for loop, why does it return a series of
one-char strings, rather than the full line?
>for sL in sys.stdin.readline(): print sL


for sL in iter(sys.stdin.readline, ''): print sL,


You just forced me to look at the documentation for iter(), which I had
previously avoided doing (on the excuse that it was confusing). Thanks
for the push!

Mike Maxwell
Jul 18 '05 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Brano Zarnovican | last post by:
Hi ! I'd like to init curses and still have working Python interactive command line. I found that you can replace stdin/stdout/stderr like this: #!/usr/bin/python -i import curses import...
6
by: Christian Convey | last post by:
Hello, I've got a program that (ideally) perpetually monitors sys.stdin for lines of text. As soon as a line comes in, my program takes some action. The problem is, it seems like a very large...
1
by: Benjamin Rutt | last post by:
There has been a problem that has been bugging me for a while for reading input from standard in. Consider the following simple program: #!/usr/bin/env python import sys print 'enter...
1
by: ywzhan | last post by:
Hi, I am new here. When I use sys.stdin.readline() to get input string from user, how can I set a timeout value for this operation? thank you.
8
by: Christoph Haas | last post by:
Hi... I encountered a problem that - according to my google search - other people have found, too. Example code: import sys for line in sys.stdin: print line Running this code in Python...
25
by: 7stud | last post by:
I can't break out of the for loop in this example: ------ import sys lst = for line in sys.stdin: lst.append(line) break
4
by: joamag | last post by:
HI, Is there any possible way to unblock the sys.stdin.readline() call from a different thread. Something like sys.stdin.write() but that would actually work ... something to put characters in...
0
by: Jean-Paul Calderone | last post by:
On Sat, 21 Jun 2008 12:35:02 -0700 (PDT), joamag <joamag@gmail.comwrote: Twisted supports asynchronous handling of stdin on both POSIX and Windows. See stdiodemo.py and stdin.py under the...
2
by: Grant Edwards | last post by:
I'm trying to figure out how to use the gnu readline library so that when my program is prompting the user for input there is line editing and history support. I've read and re-read the...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.