LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 04-12-2006, 10:41 AM   #1
bardinjw
Member
 
Registered: Apr 2003
Location: boston
Distribution: ubuntu debian redhat fedora
Posts: 108

Rep: Reputation: 15
convert while(<>) from perl to python


i have a perl script that the basic structure of which is:

Code:
$| = 1;
while (<>); {
     print;
}
I'm trying to get the equivalent funtionality in python.

Code:
import sys
while sys.stdin:
     print sys.stdin.readline()
this works the same at the command line (except ctrl+d doesn't exit), but breaks when it's in a pipe. i'm not sure if it's the stdin or stdout that's the problem.

the script needs to get a single line off the pipeline in, process it, then pipe it back out.
 
Old 04-13-2006, 06:41 AM   #2
ioerror
Member
 
Registered: Sep 2005
Location: Old Blighty
Distribution: Slackware, NetBSD
Posts: 536

Rep: Reputation: 30
Code:
while sys.stdin:
This doesn't do what you probably think! sys.stdin is an open file object, so it is always "true" (unless you close it). Python won't automatically read from sys.stdin the way Perl does with (<>).

Try something like this:

Code:
for line in sys.stdin.readlines():
        # do whatever...
        print line
or maybe this

Code:
while 1:
        line = sys.stdin.readline().strip()
        if not line:
                break
        # do whatever ....
        print line

Last edited by ioerror; 04-13-2006 at 06:46 AM.
 
Old 04-13-2006, 08:21 AM   #3
bardinjw
Member
 
Registered: Apr 2003
Location: boston
Distribution: ubuntu debian redhat fedora
Posts: 108

Original Poster
Rep: Reputation: 15
thanks ioerror

i've been doing a little more work on this
this is run as a child process, sits and waits for input on its stdin, and returns a string
Code:
read = stdin.readline
line = read()[:-1]

while line:
        if SOMETHING:
                sys.stdout.write('STRING')
                sys.stdout.flush()
        else:
                sys.stdout.write('ANOTHER STRING')
                sys.stdout.flush()
        line = read()[:-1]
It Works!
I needed to flush the output buffers, maybe this is equivalent to
"$| = 1"
in perl.

"while 1" works as well, but it won't exit gracefully when it needs to. "while line" exits in a SIGQUIT or eof.

please post any recomendations, or comments. i'm just trying to figure out the efficiencies and interactions of these little programs.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Python or Perl drdroid Programming 29 12-22-2006 09:54 AM
How to convert a float to its binary or hex representation in Python? zero79 Programming 1 09-01-2005 10:19 AM
How to convert a float to its binary or hex representation in Python? zero79 Linux - Software 1 08-29-2005 09:30 PM
convert string to integer with python Kanaflloric Programming 2 05-27-2005 11:04 AM
Perl or Python ! linuxlover1 Programming 13 04-19-2004 07:33 AM


All times are GMT -5. The time now is 10:50 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration