Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #34655 > unrolled thread
| Started by | Mike <miguelcoam@gmail.com> |
|---|---|
| First post | 2012-12-11 14:31 -0800 |
| Last post | 2012-12-11 16:44 -0800 |
| Articles | 7 — 3 participants |
Back to article view | Back to comp.lang.python
Problem with print and output to screen Mike <miguelcoam@gmail.com> - 2012-12-11 14:31 -0800
Re: Problem with print and output to screen Dave Angel <d@davea.name> - 2012-12-11 17:48 -0500
Re: Problem with print and output to screen Joel Goldstick <joel.goldstick@gmail.com> - 2012-12-11 17:53 -0500
Re: Problem with print and output to screen Dave Angel <d@davea.name> - 2012-12-11 18:00 -0500
Re: Problem with print and output to screen Joel Goldstick <joel.goldstick@gmail.com> - 2012-12-11 18:07 -0500
Re: Problem with print and output to screen Mike <miguelcoam@gmail.com> - 2012-12-11 16:44 -0800
Re: Problem with print and output to screen Mike <miguelcoam@gmail.com> - 2012-12-11 16:44 -0800
| From | Mike <miguelcoam@gmail.com> |
|---|---|
| Date | 2012-12-11 14:31 -0800 |
| Subject | Problem with print and output to screen |
| Message-ID | <37e2ba70-a709-4888-9672-be30956d690d@googlegroups.com> |
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 user@example.com
|egrep "(zimbraPrefMailForwardingAddress:|zimbraPrefMailForwardingAddress:)"
/opt/zimbra/bin/zmprov ga user2@example.com
|egrep "(zimbraPrefMailForwardingAddress:|zimbraPrefMailForwardingAddress:)"
And the correct is:
/opt/zimbra/bin/zmprov ga user@example.com |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
[toc] | [next] | [standalone]
| From | Dave Angel <d@davea.name> |
|---|---|
| Date | 2012-12-11 17:48 -0500 |
| Message-ID | <mailman.753.1355266124.29569.python-list@python.org> |
| In reply to | #34655 |
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 user@example.com
> |egrep "(zimbraPrefMailForwardingAddress:|zimbraPrefMailForwardingAddress:)"
> /opt/zimbra/bin/zmprov ga user2@example.com
> |egrep "(zimbraPrefMailForwardingAddress:|zimbraPrefMailForwardingAddress:)"
>
> And the correct is:
> /opt/zimbra/bin/zmprov ga user@example.com |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
[toc] | [prev] | [next] | [standalone]
| From | Joel Goldstick <joel.goldstick@gmail.com> |
|---|---|
| Date | 2012-12-11 17:53 -0500 |
| Message-ID | <mailman.754.1355266426.29569.python-list@python.org> |
| In reply to | #34655 |
[Multipart message — attachments visible in raw view] — view raw
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 <miguelcoam@gmail.com> 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 user@example.com
> |egrep
> "(zimbraPrefMailForwardingAddress:|zimbraPrefMailForwardingAddress:)"
> /opt/zimbra/bin/zmprov ga user2@example.com
> |egrep
> "(zimbraPrefMailForwardingAddress:|zimbraPrefMailForwardingAddress:)"
>
> And the correct is:
> /opt/zimbra/bin/zmprov ga user@example.com |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
[toc] | [prev] | [next] | [standalone]
| From | Dave Angel <d@davea.name> |
|---|---|
| Date | 2012-12-11 18:00 -0500 |
| Message-ID | <mailman.755.1355266880.29569.python-list@python.org> |
| In reply to | #34655 |
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
[toc] | [prev] | [next] | [standalone]
| From | Joel Goldstick <joel.goldstick@gmail.com> |
|---|---|
| Date | 2012-12-11 18:07 -0500 |
| Message-ID | <mailman.756.1355267237.29569.python-list@python.org> |
| In reply to | #34655 |
[Multipart message — attachments visible in raw view] — view raw
On Tue, Dec 11, 2012 at 6:00 PM, Dave Angel <d@davea.name> 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
[toc] | [prev] | [next] | [standalone]
| From | Mike <miguelcoam@gmail.com> |
|---|---|
| Date | 2012-12-11 16:44 -0800 |
| Message-ID | <e68f9cba-70a4-439a-b0c2-31014f2ab607@googlegroups.com> |
| In reply to | #34663 |
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 <d...@davea.name> 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!
[toc] | [prev] | [next] | [standalone]
| From | Mike <miguelcoam@gmail.com> |
|---|---|
| Date | 2012-12-11 16:44 -0800 |
| Message-ID | <mailman.760.1355273810.29569.python-list@python.org> |
| In reply to | #34663 |
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 <d...@davea.name> 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!
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web