473,396 Members | 1,924 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

line wrapping problem

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

Feb 9 '06 #1
7 1158

S Borg wrote:
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

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


Feb 9 '06 #2
S Borg wrote:
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.
Feb 9 '06 #3
S Borg wrote:
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?
Feb 9 '06 #4
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 :)

--
Evangelion e' la storia yaoi di un angelo che vuole portarsi a letto un
ragazzo che si intreccia con la storia di un maturo professionista con il
complesso delle lolite... fatte in casa.
-- Lennier, 2006

Feb 9 '06 #5
S Borg wrote:
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

Feb 9 '06 #6
ZeD wrote:
Ciao, Juho Schultz! Che stavi dicendo?
Moro, ZeD! Kunhan pulisen. Should we stick to English?
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 :)

(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.
Feb 9 '06 #7
S Borg wrote:
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
Feb 9 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Nick | last post by:
Im developing php pages under apache and Mac OS X. When I have a scripting error, the error page says... ....error occurred at line 139 Problem is that line 139 is blank and the bad code is...
10
by: Steve | last post by:
I'm not sure if I should be using tables here to structure the layout or if CSS is okay. I have a data entry form in which I have floated the labels to one side, and given them a specific width....
11
by: name | last post by:
Here is a first attempt at a line/word wrapping utility. Seems to work okay, but lacks some checking stuff, etc. --------------------------------------------------------- #include <stdio.h>...
1
by: mg | last post by:
I set the width of a WebForm Label but do not get line wrapping (see below). What can I do to get line wrapping WebForm1.asp <asp:Labe id="Label1" style="Z-INDEX: 101; LEFT: 40px; POSITION:...
3
by: David Eadie | last post by:
G'Day all, I cant for the God in me work out how to execute the following command line in vb.net: (watch for the word wrapping also) Sub(ByVal username As String) Shell("ECHO Y| cacls...
6
by: Markus Ernst | last post by:
Hi Searching for a possibility to display some text with preserved white space and line breaks, but with long lines being wrapped, I found this CSS declaration, which I found helpful: pre {...
5
by: Jason | last post by:
I have a div box with a border, and some padding left and right. It has some text with different font sizes, and a few icons. I want to cut off any overflow at the end of the line with "...",...
7
by: John den Haan | last post by:
Hello! When I use putchar to fill up an entire screen (of 80x25) with text, it seems to leave an empty line at the end, thus forcing me to scroll upwards in to see the first line. This forces me...
7
by: lister | last post by:
Is this possible? I want fixed height table rows, but long text is wrapping around and causing the cell, and thus the entire row, to increase in height. I want any long text to simply...
10
by: marss | last post by:
<hr style="height:1px"/does not fit because although a line looks like it has 1 pixel with but there are empty spaces above and below line. It can be seen if place two HR elements beside. <hr...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.