473,548 Members | 2,716 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

retaining newline characters when writing to file

If I read a string that contains a newline character(s) into a variable,
then write that variable to a file, how can I retain those newline
characters so that the string remains on one line rather than spans
multiple lines?

Example: In a CGI script, I'm reading the address field from an HTML
form. Almost always the person will press ENTER so they can enter a
second or third line. If I then want to write all the field values of
this form to a CSV file, so that each person's entries takes one line,
how can I ensure that the address string stays on one line despite the
newline characters?

(Or is it better to just use separate text fields for each line of the
address?)

Thanks.
Jun 6 '06 #1
2 5660
John Salerno wrote:
If I read a string that contains a newline character(s) into a variable,
then write that variable to a file, how can I retain those newline
characters so that the string remains on one line rather than spans
multiple lines?


you cannot: the whole point of a newline character is to start a new line.

however, some file formats let you "escape" the newline. for example,
in Python source code, you can use end a line with a backslash. in CSV,
you can put the string with newlines inside quotes, and Python's "csv"
module knows how to do that:

import csv, sys

row = ("One\nTwo\nThr ee", 1, 2, 3)

writer = csv.writer(sys. stdout)
writer.writerow (row)

prints

"One
Two
Three",1,2,3

(not all CSV readers can handle multiline rows, though)

</F>

Jun 6 '06 #2
Fredrik Lundh wrote:
John Salerno wrote:
If I read a string that contains a newline character(s) into a
variable, then write that variable to a file, how can I retain those
newline characters so that the string remains on one line rather than
spans multiple lines?


you cannot: the whole point of a newline character is to start a new line.

however, some file formats let you "escape" the newline. for example,
in Python source code, you can use end a line with a backslash. in CSV,
you can put the string with newlines inside quotes, and Python's "csv"
module knows how to do that:

import csv, sys

row = ("One\nTwo\nThr ee", 1, 2, 3)

writer = csv.writer(sys. stdout)
writer.writerow (row)

prints

"One
Two
Three",1,2,3

(not all CSV readers can handle multiline rows, though)

</F>


Thanks. I should give the csv module a look too, while I'm at it.
Jun 6 '06 #3

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

Similar topics

1
5427
by: av | last post by:
hi all, i am trying to transfer a text file from client to server. say, my text file on the client side is named like this "afile.txt": on the server side, am storing it by different name i.e "bfile.txt" "afile.txt" 1 djfldjf"\n" 2 dslfjlsdjf"\n"
5
3400
by: SunSmile | last post by:
Hi, I am logging my exceptions to a word document(*.doc). After the size of word document is 5KB. I am archiving the word document to *.doc.1 Here when I am archiving the word document to *.doc.1, the new line characters("\r\n") are lost and the *.doc.1 document is instead displaying a null square space. I am using Log4Net to log the...
29
3421
by: runningdog | last post by:
Hi, I would like to be able to embed a newline in a text string. Is there any convienent notation to do this TIA Steve
1
1784
by: Nick Wain | last post by:
Hello All, I'm relatively new to PYTHON, using PYTHON 2.4 on Windows XP. I'm having a problem as below. I've asked some other people in my office who are more experienced in PYTHON, but they can't help. I have a number of files created in UNIX that have the UNIX end of line (EOL) character. I want to read these files in python, modify...
3
2854
by: utab | last post by:
Dear all, I have a text file in the form of 1 2 3 4 5 6 --------/--------/--------/--------/--------/--------/ (I just used / to better explain the fields) 6 fields of 8 characters. I want to read lets say fields 4,5 and 6 and then go to the begining of the newline which is in the same format....
1
3021
by: utab | last post by:
Hi there I am trying to read from a file, I am trying to read certain fields,there are 6 fields in this file like --------/--------/--------/--------/--------/--------/ All fields are 8 characters width and I am comparing the first character of the line with the $ sign which seperates different blocks of information $ Nodes of the...
5
6980
by: Adam Right | last post by:
Hi, Is there a way to construct the mail body including newline characters by using .net framework mailing functions when sending an email? I cannot insert newline character into the body of the mail. Thanks...
1
8491
by: linq936 | last post by:
Hi, I read in many places that the string to be outputted by printf() must be ending with newline, for example, it should be printf("Hello World.\n"); instead of printf("Hello World.");
3
11214
by: ofilha | last post by:
I am trying to export the resultset of a query to a character delimited file. I am using the wizard for this. However, one of the fields is 512 characters long and it accepts from form address. It looks like the field contains newlines, but when i look at the resultset in DB2 i don't see any newline characters, only after i copy it and then...
0
7518
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7444
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7954
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7805
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...
0
6039
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...
0
5085
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3478
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1054
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
755
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...

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.