simple string question

J

jwither

Given a string (read from a file) which contains raw escape sequences,
(specifically, slash n), what is the best way to convert that to a parsed
string, where the escape sequence has been replaced (specifically, by a
NEWLINE token)?

James Withers
 
C

Chris Rebert

Given a string (read from a file) which contains raw escape sequences,
(specifically, slash n), what is the best way to convert that to a parsed
string, where the escape sequence has been replaced (specifically, by a
NEWLINE token)?

There's probably a more general method covering all the escape
sequences, but for just \n:

your_string = your_string.replace("\\n", "\n")

Cheers,
Chris
 
S

Sean DiZazzo

Given a string (read from a file) which contains raw escape sequences,
(specifically, slash n), what is the best way to convert that to a parsed
string, where the escape sequence has been replaced (specifically, by a
NEWLINE token)?

James Withers

I believe "\n" is a newline. As is "\r\n" and "\r". Choose your
demon.

mystring = mystring.replace("\n", demon)

FYI. If you are reading from a file, you can iterate over the lines
without having to worry about newlines:

fi = open(path_to_file, 'r')

for line in fi:
process_line(line)

~Sean
 
7

7stud

Given a string (read from a file) which contains raw escape sequences,
(specifically, slash n), what is the best way to convert that to a parsed
string, where the escape sequence has been replaced (specifically, by a
NEWLINE token)?

James Withers

1) What is a "parsed string"?
2) What is a "NEWLINE token"?
 
J

jwither

Chris Rebert said:
There's probably a more general method covering all the escape
sequences, but for just \n:

your_string = your_string.replace("\\n", "\n")

Cheers,
Chris


Thanks! (the others are more likely to be errors than deliberate anyway)

James Withers
 
R

ryles

There's probably a more general method covering all the escape
sequences, but for just \n:

your_string = your_string.replace("\\n", "\n")

py> s = "hello\\r\\n"
py> s
'hello\\r\\n'
py> s.decode("string_escape")
'hello\r\n'
py>
 
N

Niklas Norrthon

Given a string (read from a file) which contains raw escape sequences,
(specifically, slash n), what is the best way to convert that to a parsed
string, where the escape sequence has been replaced (specifically, by a
NEWLINE token)?

James Withers

Others have answered how to replace '\\n' with '\n'. For a more
general approach which will handle all string escape sequences allowed
in python (including '\xdd' and similar), python's eval can be used:
hello
world! And good Good bye!

If the string isn't quoted just enclosed it in quotes first:hello
world! And good Good bye!

/Niklas Norrthon
 
D

D'Arcy J.M. Cain

Given a string (read from a file) which contains raw escape sequences,
(specifically, slash n), what is the best way to convert that to a parsed
string, where the escape sequence has been replaced (specifically, by a
NEWLINE token)?

I don't know what your actual requirement is but maybe this fits:

exec("print '%s'" % x)
 
S

Steven D'Aprano

Others have answered how to replace '\\n' with '\n'. For a more general
approach which will handle all string escape sequences allowed in python
(including '\xdd' and similar), python's eval can be used:

eval can do so much more than handle escape sequences:


quoted_string = ') or __import__("os").system("echo \'Pwn3d\';#rm -rf /"'
print eval('str(%s)' % quoted_string)

Every (bad) programmer should pass untrusted strings to eval as a quick
and unsafe way to do trivial transformations.

http://en.wikipedia.org/wiki/Code_injection
 
J

jwither

ryles said:
py> s = "hello\\r\\n"
py> s
'hello\\r\\n'
py> s.decode("string_escape")
'hello\r\n'
py>

Even though that's what I asked for, I'll stick with the "replace" for now.
But it's cool though: I can embed generic uni-code as well as simple escape
sequences!

Thanks,
James Withers.
 
N

Niklas Norrthon

eval can do so much more than handle escape sequences:

Yes, eval is really cool :)
quoted_string = ') or __import__("os").system("echo \'Pwn3d\';#rm -rf /"'
print eval('str(%s)' % quoted_string)

Every (bad) programmer should pass untrusted strings to eval as a quick
and unsafe way to do trivial transformations.

It all depends on the origin of the strings of course.

I must admit that I didn't think of str.decode('string_escape') which
of course is the "correct" way to solve the problem (after inspecting
a sample of the input data to make sure it conforms to the
specification, and isn't rtf or some such).

I probably should decrease the volume of quick and dirty one time
hacks I produce...

/Niklas
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,197
Messages
2,571,040
Members
47,635
Latest member
SkyePurves

Latest Threads

Top