469,644 Members | 1,758 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,644 developers. It's quick & easy.

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 5485
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\nThree", 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\nThree", 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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by av | last post: by
29 posts views Thread by runningdog | last post: by
1 post views Thread by Nick Wain | last post: by
3 posts views Thread by utab | last post: by
5 posts views Thread by Adam Right | last post: by
1 post views Thread by linq936 | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.