By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,965 Members | 1,779 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,965 IT Pros & Developers. It's quick & easy.

new line

P: n/a
Dear friends
I'm so sorry to bore you with this trivial problem. Allthou: I have string
having 0x0a as new line, but I should have \n instead.
How should I solve it?
I've tried
text_new=tex_old.replace(str(0x0a), '\n')

and other things, but none of them worked.
Thanks in advance
Aug 29 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
"Kuljo" <ku***@freemail.hu> schrieb im Newsbeitrag
news:K1*******************@nntpserver.swip.net...
Dear friends
I'm so sorry to bore you with this trivial problem. Allthou: I have string
having 0x0a as new line, but I should have \n instead.
How should I solve it?
I've tried
text_new=tex_old.replace(str(0x0a), '\n')

and other things, but none of them worked.
Thanks in advance


text_new=tex_old.replace(chr(0x0a), '\r\n')
works for me.

Claudio
P.S. str(0x0a) == '10'
Aug 29 '05 #2

P: n/a
Kuljo wrote:
Dear friends
I'm so sorry to bore you with this trivial problem. Allthou: I have string
having 0x0a as new line, but I should have \n instead.


In [9]: '\x0a'
Out[9]: '\n'

They're the same thing.

--
Robert Kern
rk***@ucsd.edu

"In the fields of hell where the grass grows high
Are the graves of dreams allowed to die."
-- Richard Harter

Aug 29 '05 #3

P: n/a
Kuljo wrote:
Dear friends
I'm so sorry to bore you with this trivial problem. Allthou: I have string
having 0x0a as new line, but I should have \n instead.
How should I solve it?
I've tried
text_new=tex_old.replace(str(0x0a), '\n') and other things, but none of them worked.
Thanks in advance

I have found this in the meantime:
nl="\\"+"n"
text_new=replace(text_old, chr(10), nl)


It works.
Aug 30 '05 #4

P: n/a
Kuljo wrote:
Kuljo wrote:
I'm so sorry to bore you with this trivial problem. Allthou: I have string
having 0x0a as new line, but I should have \n instead.
I have found this in the meantime:nl="\\"+"n"
Note: this is unnecessary. You could just do nl='\\n' instead, and you
don't need the variable "nl" either, which by the way is confusingly
named. Your variable "nl" is actually bound to the two character
sequence \ followed by n instead of a "newline". Is that really what
you wanted?
text_new=replace(text_old, chr(10), nl)
This use of replace() is deprecated. Use text_old.replace() instead.
It works.


For some definitions of "works". This only works if you wanted the
*single* character represented by '\x0a' to turn into the *two*
characters backslash-followed-by-letter-n. The following you might find
instructive:
old = 'some\x0atext'
old 'some\ntext' print old some
text old.encode('string-escape') 'some\\ntext' print old.encode('string-escape')

some\ntext

This will turn other control characters into their equivalent
two-character escaped representation as well, such as \t and \r, as
necessary.

-Peter
Aug 30 '05 #5

P: n/a
Kuljo wrote:
Kuljo wrote:
Dear friends
I'm so sorry to bore you with this trivial problem. Allthou: I have
string having 0x0a as new line, but I should have \n instead.
How should I solve it?
I've tried
>text_new=tex_old.replace(str(0x0a), '\n')

and other things, but none of them worked.
Thanks in advance

I have found this in the meantime:
nl="\\"+"n"
text_new=replace(text_old, chr(10), nl)
It works.


Thanks Peter,
you are right, even
text_new=replace(text_old, chr(10), "\\n")

works.
Yes, it was exactly what I needed. The problem actually was: I'm using Pilot
V for years and a part of my "knowledge base" is stored in den memo files.
On the PC side I'm using jpilot (Linux Kubuntu 5.04) which works perfectly.
Now I wanted to try out kontact (groupware). The shortcoming is that
kontact (actually kpilot in the background) does not import the memos. But
it store them in a folder as a file per memo. In this file are the cr/lf
indicated by hex 0a. The notes in kontact uses char \n instead. For this
translation I made a python script which works now.
Kind regards Kuljo
Aug 31 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.