[Tutor] Add newline's, wrap, a long string
A.T.Hofkamp
a.t.hofkamp at tue.nl
Wed Apr 29 09:48:17 CEST 2009
David wrote:
> I am getting information from .txt files and posting them in fields on a
> web site. I need to break up single strings so they are around 80
> characters then a new line because when I enter the info to the form on
> the website it has fields and it errors out with such a long string.
>
> here is a sample of the code;
>
> #!/usr/bin/python
> import subprocess
> import os
>
> u_e = subprocess.Popen(
> 'grep USE /tmp/comprookie2000/emerge_info.txt |head -n1|cut -d\\"-f2',
> shell=True, stdout=subprocess.PIPE,)
> os.waitpid(u_e.pid, 0)
> USE = u_e.stdout.read().strip()
Why not do the above command sequence entirely in Python instead of this
sub-process trickery with its shell quoting problems?
It seems a much nicer solution to me.
> L = len(USE)
> print L
> print USE
>
> L returns 1337
First stop for any problem that you have: The Python standard library.
A search told me that Python has a textwrap module in stdlib.
Would that be of use to you?
Albert
More information about the Tutor
mailing list