Problem with print and output to screen

Discussion in 'Python' started by Mike, Dec 11, 2012.

  1. Mike

    Mike Guest

    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
    |egrep "(zimbraPrefMailForwardingAddress:|zimbraPrefMailForwardingAddress:)"
    /opt/zimbra/bin/zmprov ga
    |egrep "(zimbraPrefMailForwardingAddress:|zimbraPrefMailForwardingAddress:)"

    And the correct is:
    /opt/zimbra/bin/zmprov ga |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
    Mike, Dec 11, 2012
    #1
    1. Advertising

  2. Mike

    Dave Angel Guest

    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
    > |egrep "(zimbraPrefMailForwardingAddress:|zimbraPrefMailForwardingAddress:)"
    > /opt/zimbra/bin/zmprov ga
    > |egrep "(zimbraPrefMailForwardingAddress:|zimbraPrefMailForwardingAddress:)"
    >
    > And the correct is:
    > /opt/zimbra/bin/zmprov ga |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.



    --

    DaveA
    Dave Angel, Dec 11, 2012
    #2
    1. Advertising

  3. 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 <> 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
    > |egrep
    > "(zimbraPrefMailForwardingAddress:|zimbraPrefMailForwardingAddress:)"
    > /opt/zimbra/bin/zmprov ga
    > |egrep
    > "(zimbraPrefMailForwardingAddress:|zimbraPrefMailForwardingAddress:)"
    >
    > And the correct is:
    > /opt/zimbra/bin/zmprov ga |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
    > --
    > http://mail.python.org/mailman/listinfo/python-list
    >




    --
    Joel Goldstick
    Joel Goldstick, Dec 11, 2012
    #3
  4. Mike

    Dave Angel Guest

    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
    Dave Angel, Dec 11, 2012
    #4
  5. On Tue, Dec 11, 2012 at 6:00 PM, Dave Angel <> 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
    Joel Goldstick, Dec 11, 2012
    #5
  6. Mike

    Mike Guest

    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 <> 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!
    Mike, Dec 12, 2012
    #6
  7. Mike

    Mike Guest

    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 <> 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!
    Mike, Dec 12, 2012
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.

Share This Page