[Tutor] Add newline's, wrap, a long string
Martin Walsh
mwalsh at mwalsh.org
Wed Apr 29 06:06:59 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',
Did you copy and paste this faithfully? The 'cut -d\\"-f2' looks a bit
odd. Is the delimiter a " (double-quote)? Perhaps you left out a space
before the -f2?
> shell=True, stdout=subprocess.PIPE,)
> os.waitpid(u_e.pid, 0)
'u_e.wait()' would wait the way you intend, as well, I believe.
> USE = u_e.stdout.read().strip()
Or, if you use the communicate() method of the Popen object, the wait is
implicit. As in,
stdout, stderr = u_e.communicate()
... or perhaps ...
USE = u_e.communicate()[0].strip()
... but, you don't need to use subprocess at all. How about (untested),
# grep USE /tmp/comprookie2000/emerge_info.txt |head -n1|cut -d\" -f2
infof = open('/tmp/comprookie2000/emerge_info.txt')
for line in infof:
if 'USE' in line:
USE = line.split('"')[1]
break
else:
USE = ''
infof.close()
> L = len(USE)
> print L
> print USE
>
> L returns 1337
cosmic :)
HTH,
Marty
More information about the Tutor
mailing list