line wrapping problem

S

S Borg

Hello,

I am parsing text from one document to another. I have a scheme
similar to:

for x in myfoobar:
print >> mytextfile, "%s " % mydictionary[x], #all on same line
print >> mytextfile, '\n' #new line


I am getting line breaks before my explicit line break. Am I
unwittingly copying '\n' characters from the original file?
How should I fix this(What is the 'Pythonic' solution)?

thanks,
-S
 
F

Fuzzyman

S said:
Hello,

I am parsing text from one document to another. I have a scheme
similar to:

for x in myfoobar:
print >> mytextfile, "%s " % mydictionary[x], #all on same line
print >> mytextfile, '\n' #new line

You are using the print command to output the text to your file. This
will always put a '\n' at the end of lines (as well as your explicit)
one. The only time it doesn't do this is if the line ends with a
trailing comma, in which case it just adds a space.

The usual way is to write to yhour file object using :

mytextfile.write( mytextfile, "%s " % mydictionary[x],)

All the best,

Fuzzyman
http://www.voidspace.org.uk/python/index.shtml
 
J

Juho Schultz

S said:
Hello,

I am parsing text from one document to another. I have a scheme
similar to:

for x in myfoobar:
print >> mytextfile, "%s " % mydictionary[x], #all on same line
print >> mytextfile, '\n' #new line


I am getting line breaks before my explicit line break. Am I
unwittingly copying '\n' characters from the original file?
How should I fix this(What is the 'Pythonic' solution)?

thanks,
-S

mytextfile.write("%s " % mydictionary[x])

should work. IMO file.write() is self-explanatory but "print >> file" is
a bit obscure.
 
J

John Zenger

S said:
print >> mytextfile, '\n' #new line

Wouldn't this print two line breaks--the one you specify in the string,
plus the one always added by print if there is no comma at the end of
the statement?
 
Z

ZeD

Ciao, Juho Schultz! Che stavi dicendo?
should work. IMO file.write() is self-explanatory but "print >> file" is
a bit obscure.

is obscure only if you have never used a shell :)
 
P

Peter Otten

S said:
I am parsing text from one document to another. I have a scheme
similar to:

for x in myfoobar:
print >> mytextfile, "%s " % mydictionary[x], #all on same line

print >> mytextfile # minimal fix
I am getting line breaks before my explicit line break. Am I
unwittingly copying '\n' characters from the original file?
How should I fix this(What is the 'Pythonic' solution)?

The trailing spaces are probably an artifact of your implementation. Here's
one way to avoid them:

print >> mytextfile, " ".join(str(mydictionary[x]) for x in myfoobar)

Peter
 
J

Juho Schultz

ZeD said:
Ciao, Juho Schultz! Che stavi dicendo?
Moro, ZeD! Kunhan pulisen. Should we stick to English?
is obscure only if you have never used a shell :)
(I have used the shell a bit. I started using Linux at work when 2.2
series kernels did not exist.)

Assume a newbie plays around with a piece code. If the code has
f.write(x) or print >> f,x - in which case the newbie is more likely to
break the code by rebinding f to something non-file? And in which case
he will more likely understand the error message, something like "f has
no write attribute"?

I am sure the >> can be useful - it is quick to add after a simple print
when you want less chatter and more logfiles. But IMO it is not
self-documenting. Having to very different uses for an operator in the
same language is sort of confusing.
 
K

Kent Johnson

S said:
Hello,

I am parsing text from one document to another. I have a scheme
similar to:

for x in myfoobar:
print >> mytextfile, "%s " % mydictionary[x], #all on same line
print >> mytextfile, '\n' #new line


I am getting line breaks before my explicit line break. Am I
unwittingly copying '\n' characters from the original file?

Where does mydictionary[x] come from? If it is from reading lines from a
file it may contain trailing newlines.

Also
print >> mytextfile, '\n'
will print two newlines, one explicit and one implicit in the print.

Kent
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,283
Messages
2,571,409
Members
48,102
Latest member
charleswillson

Latest Threads

Top