473,765 Members | 1,955 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1169

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.writ e( 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.writ e("%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(mydi ctionary[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
2740
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 really on line 142. Its usaully off by random numbers so I cant just add/subtract a couple of numbers to guess the real line of problem code. This is a pain and is slowing me up for trouble-shooting scripts. Any suggestions? Is this a php.ini...
10
11459
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. With regular input such as textboxes, everything lines up fine. The problem I'm having is that I have an unordered list of checkboxes, and only the first item will line up with the label. The additional checkboxes and their labels end up being...
11
6080
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> #include <stdlib.h> #include <string.h> #include <limits.h>
1
1766
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: absolute; TOP: 48px runat="server" Width="150px" Height="64px"></asp:Label
3
6282
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 h:\finance\" & username & "\" & "+Common /G " & username & ":R", AppWinStyle.Hide, True) End Sub
6
1165
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 { white-space: pre-wrap; /* css-3 */ white-space: -moz-pre-wrap; /* Mozilla, since 1999 */ white-space: -pre-wrap; /* Opera 4-6 */
5
6410
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 "...", instead of the text wrapping. What is the best way I can detect whether the line wrapped, and how many characters to cut off at the end? - Jason
7
2445
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 to repositioning the cursor to the first line, which costs computer power. I know the loss is negligible, but it's more a matter of principle: how to prevent this line-wrapping behaviour? --
7
13142
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 truncate. Many thanks, Lister
10
6977
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 style="height:1px"/> <hr style="height:1px"/> I tried also <div style="height:1px;font-size:1px;background- color:black;"></divbut a line was 2 pixel thick :(
0
10160
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9951
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9832
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8831
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7378
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5275
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3924
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3531
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2805
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.