Problem with print and output to screen

M

Mike

Hello, I am learning python and i have the next problem and i not understand how fix.
The script is very simple, shows in the terminal the command but, the row is divided in two:
Example:


/opt/zimbra/bin/zmprov ga (e-mail address removed)
|egrep "(zimbraPrefMailForwardingAddress:|zimbraPrefMailForwardingAddress:)"
/opt/zimbra/bin/zmprov ga (e-mail address removed)
|egrep "(zimbraPrefMailForwardingAddress:|zimbraPrefMailForwardingAddress:)"

And the correct is:
/opt/zimbra/bin/zmprov ga (e-mail address removed) |egrep "(zimbraPrefMailForwardingAddress:|zimbraPrefMailForwardingAddress:)"


The script is:

#!/usr/bin/python
import os

for user in open ("email"):
print '/opt/zimbra/bin/zmprov ga ' + user + '|egrep "(zimbraPrefMailForwardingAddress:|zimbraPrefMailForwardingAddress:)" '



Thanks
 
D

Dave Angel

Hello, I am learning python and i have the next problem and i not understand how fix.
The script is very simple, shows in the terminal the command but, the row is divided in two:
Example:


/opt/zimbra/bin/zmprov ga (e-mail address removed)
|egrep "(zimbraPrefMailForwardingAddress:|zimbraPrefMailForwardingAddress:)"
/opt/zimbra/bin/zmprov ga (e-mail address removed)
|egrep "(zimbraPrefMailForwardingAddress:|zimbraPrefMailForwardingAddress:)"

And the correct is:
/opt/zimbra/bin/zmprov ga (e-mail address removed) |egrep "(zimbraPrefMailForwardingAddress:|zimbraPrefMailForwardingAddress:)"


The script is:

#!/usr/bin/python
import os

for user in open ("email"):
print '/opt/zimbra/bin/zmprov ga ' + user + '|egrep "(zimbraPrefMailForwardingAddress:|zimbraPrefMailForwardingAddress:)" '

I'll assume that 'email' is a file in the current working directory. So
when you open it and iterate through it, each line will be stored in
'user', including its trailing newline.

When you print 'user', you're seeing the newline.

To see for yourself, you could/should have used (just before your print
statement)

print repr(user)

which will show such things as escape sequences.

Anyway, the easiest way to fix it would be to use rstrip() on the line.

for user in open("email"):
user = user.rstrip()
print '/opt/...

That's assuming there's no trailing whitespace that you DO want to preserve.
 
J

Joel Goldstick

When you read the file line by line the end of line character is included
in the result

try user[:-1] instead to strip the return from your printed text
 
D

Dave Angel

When you read the file line by line the end of line character is included
in the result

try user[:-1] instead to strip the return from your printed text

The catch to that is the last line in the file might not have a
newline. In that case, we'd be ignoring the last character of the line.

The .rstrip() method is easy, and for most purposes equivalent. Few
text files have trailing whitespace, but many are missing the final
linefeed.
 
J

Joel Goldstick

When you read the file line by line the end of line character is included
in the result

try user[:-1] instead to strip the return from your printed text

The catch to that is the last line in the file might not have a
newline. In that case, we'd be ignoring the last character of the line.

The .rstrip() method is easy, and for most purposes equivalent. Few
text files have trailing whitespace, but many are missing the final
linefeed.

Point taken. Brain freeze. I forgot about .rstrip. That is the way to go
 
M

Mike

El martes, 11 de diciembre de 2012 20:07:09 UTC-3, Joel Goldstick escribió:
When you read the file line by line the end of line character is included
in the result
try user[:-1] instead to strip the return from your printed text



The catch to that is the last line in the file might not have a

newline.  In that case, we'd be ignoring the last character of the line..



The .rstrip() method is easy, and for most purposes equivalent.  Few

text files have trailing whitespace, but many are missing the final

linefeed.



Point taken.  Brain freeze.  I forgot about .rstrip.  That is the way to go



--



DaveA

Thank you very much, i used "user.rstrip" and the output is correct .

Best Regards!
 
M

Mike

El martes, 11 de diciembre de 2012 20:07:09 UTC-3, Joel Goldstick escribió:
When you read the file line by line the end of line character is included
in the result
try user[:-1] instead to strip the return from your printed text



The catch to that is the last line in the file might not have a

newline.  In that case, we'd be ignoring the last character of the line..



The .rstrip() method is easy, and for most purposes equivalent.  Few

text files have trailing whitespace, but many are missing the final

linefeed.



Point taken.  Brain freeze.  I forgot about .rstrip.  That is the way to go



--



DaveA

Thank you very much, i used "user.rstrip" and the output is correct .

Best Regards!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top