Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Problem with print and output to screen

Reply
Thread Tools

Problem with print and output to screen

 
 
Mike
Guest
Posts: n/a
 
      12-11-2012
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 http://www.velocityreviews.com/forums/(E-Mail Removed)
|egrep "(zimbraPrefMailForwardingAddresszimbraPrefMailF orwardingAddress"
/opt/zimbra/bin/zmprov ga (E-Mail Removed)
|egrep "(zimbraPrefMailForwardingAddresszimbraPrefMailF orwardingAddress"

And the correct is:
/opt/zimbra/bin/zmprov ga (E-Mail Removed) |egrep "(zimbraPrefMailForwardingAddresszimbraPrefMailF orwardingAddress"


The script is:

#!/usr/bin/python
import os

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



Thanks
 
Reply With Quote
 
 
 
 
Dave Angel
Guest
Posts: n/a
 
      12-11-2012
On 12/11/2012 05:31 PM, Mike wrote:
> 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 Removed)
> |egrep "(zimbraPrefMailForwardingAddresszimbraPrefMailF orwardingAddress"
> /opt/zimbra/bin/zmprov ga (E-Mail Removed)
> |egrep "(zimbraPrefMailForwardingAddresszimbraPrefMailF orwardingAddress"
>
> And the correct is:
> /opt/zimbra/bin/zmprov ga (E-Mail Removed) |egrep "(zimbraPrefMailForwardingAddresszimbraPrefMailF orwardingAddress"
>
>
> The script is:
>
> #!/usr/bin/python
> import os
>
> for user in open ("email"):
> print '/opt/zimbra/bin/zmprov ga ' + user + '|egrep "(zimbraPrefMailForwardingAddresszimbraPrefMailF orwardingAddress" '
>
>
>


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.



--

DaveA

 
Reply With Quote
 
 
 
 
Joel Goldstick
Guest
Posts: n/a
 
      12-11-2012
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


On Tue, Dec 11, 2012 at 5:31 PM, Mike <(E-Mail Removed)> wrote:

> 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 Removed)
> |egrep
> "(zimbraPrefMailForwardingAddresszimbraPrefMailF orwardingAddress"
> /opt/zimbra/bin/zmprov ga (E-Mail Removed)
> |egrep
> "(zimbraPrefMailForwardingAddresszimbraPrefMailF orwardingAddress"
>
> And the correct is:
> /opt/zimbra/bin/zmprov ga (E-Mail Removed) |egrep
> "(zimbraPrefMailForwardingAddresszimbraPrefMailF orwardingAddress"
>
>
> The script is:
>
> #!/usr/bin/python
> import os
>
> for user in open ("email"):
> print '/opt/zimbra/bin/zmprov ga ' + user + '|egrep
> "(zimbraPrefMailForwardingAddresszimbraPrefMailF orwardingAddress" '
>
>
>
> Thanks
> --
> http://mail.python.org/mailman/listinfo/python-list
>




--
Joel Goldstick

 
Reply With Quote
 
Dave Angel
Guest
Posts: n/a
 
      12-11-2012
On 12/11/2012 05:53 PM, Joel Goldstick wrote:
> 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.



--

DaveA

 
Reply With Quote
 
Joel Goldstick
Guest
Posts: n/a
 
      12-11-2012
On Tue, Dec 11, 2012 at 6:00 PM, Dave Angel <(E-Mail Removed)> wrote:

> On 12/11/2012 05:53 PM, Joel Goldstick wrote:
> > 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
>
>



--
Joel Goldstick

 
Reply With Quote
 
Mike
Guest
Posts: n/a
 
      12-12-2012
El martes, 11 de diciembre de 2012 20:07:09 UTC-3, Joel Goldstick escribió:
> On Tue, Dec 11, 2012 at 6:00 PM, Dave Angel <(E-Mail Removed)> wrote:
>
>
> On 12/11/2012 05:53 PM, Joel Goldstick wrote:
>
> > 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
>
>
>
>
>
>
> --
> Joel Goldstick


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

Best Regards!
 
Reply With Quote
 
Mike
Guest
Posts: n/a
 
      12-12-2012
El martes, 11 de diciembre de 2012 20:07:09 UTC-3, Joel Goldstick escribió:
> On Tue, Dec 11, 2012 at 6:00 PM, Dave Angel <(E-Mail Removed)> wrote:
>
>
> On 12/11/2012 05:53 PM, Joel Goldstick wrote:
>
> > 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
>
>
>
>
>
>
> --
> Joel Goldstick


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

Best Regards!
 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Want help on how we convert output to tabular format Using the expat parser (http://expat.sourceforge.net/) i have to parse the following xml file and print it on the screen in tabular format. sharan XML 1 10-26-2007 01:20 PM
Re: Problem - I want to print Current Output of Pdf file and shouldprint once.I get print dialog box but it is not working Lew Java 0 05-30-2007 01:31 PM
Problem - I want to print Current Output of Pdf file and should print once.I get print dialog box but it is not working keto Java 0 05-30-2007 11:27 AM
Lock Print Screen and File Print Bryan Shaw ASP .Net 3 04-20-2005 02:54 PM
Newbie: How do I filter output to the screen and writing the orginal output to a file? Mav Perl Misc 22 07-09-2004 09:56 PM



Advertisments