Escaping certain characters

J

Jan Danielsson

Hello,

I'd like to encode the string that outputs:

Hello
World!

to 'Hello\x0aWorld!', and the string that outputs:

Hello\World!

to 'Hello\\World!'.

Obviously, I want to be able to reverse the process.

I'm going to assume this has already been solved in Python.. But how?
 
R

Robert Kern

Jan said:
Hello,

I'd like to encode the string that outputs:

Hello
World!

to 'Hello\x0aWorld!', and the string that outputs:

Hello\World!

to 'Hello\\World!'.

Obviously, I want to be able to reverse the process.

I'm going to assume this has already been solved in Python.. But how?

In [1]: s = 'Hello\x0aWorld!'

In [2]: print s
Hello
World!

In [3]: s.encode('string_escape')
Out[3]: 'Hello\\nWorld!'

In [4]: Out[3].decode('string_escape')
Out[4]: 'Hello\nWorld!'

Not *quite* what you asked for, but it ought to be close enough.

--
Robert Kern
(e-mail address removed)

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

Jan Danielsson

Robert Kern wrote:
[---]
In [3]: s.encode('string_escape')
Out[3]: 'Hello\\nWorld!'

In [4]: Out[3].decode('string_escape')
Out[4]: 'Hello\nWorld!'

Not *quite* what you asked for, but it ought to be close enough.

That'll do just fine. Many thanks!
 
J

Jan Danielsson

Jan said:
In [3]: s.encode('string_escape')
Out[3]: 'Hello\\nWorld!'

In [4]: Out[3].decode('string_escape')
Out[4]: 'Hello\nWorld!'

Not *quite* what you asked for, but it ought to be close enough.

That'll do just fine. Many thanks!

Hmm... On second thought, I need to escape more characters.

Is there no other way to escape characters in strings?
 
R

Robert Kern

Jan said:
Jan said:
In [3]: s.encode('string_escape')
Out[3]: 'Hello\\nWorld!'

In [4]: Out[3].decode('string_escape')
Out[4]: 'Hello\nWorld!'

Not *quite* what you asked for, but it ought to be close enough.

That'll do just fine. Many thanks!

Hmm... On second thought, I need to escape more characters.

Is there no other way to escape characters in strings?

Which characters?

--
Robert Kern
(e-mail address removed)

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

Jan Danielsson

Robert Kern wrote:
[---]
Which characters?

I need to escape '\n', '"', '[' and ']'. I finally went with a few of
these:
string.replace('\n', '\\n')
string.replace('"', '\\"')
....

I assume that's good enough, but I somehow expected there to exist
some form of "insert your conversion table here" built-in string escaper.
 
R

Robert Kern

Jan said:
Robert Kern wrote:
[---]
Which characters?

I need to escape '\n', '"', '[' and ']'. I finally went with a few of
these:
string.replace('\n', '\\n')
string.replace('"', '\\"')
...

I assume that's good enough, but I somehow expected there to exist
some form of "insert your conversion table here" built-in string escaper.

Write a codec.

--
Robert Kern
(e-mail address removed)

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

Jan Danielsson

Robert Kern wrote:
[---]
I need to escape '\n', '"', '[' and ']'. I finally went with a few of
these:
string.replace('\n', '\\n')
string.replace('"', '\\"')
...

I assume that's good enough, but I somehow expected there to exist
some form of "insert your conversion table here" built-in string escaper.

Write a codec.

Is that what string.encode() uses? Some behind-the-scenes "codec"? I
tried searching for it, but could only find UTF/Unicode-related
information. Is it in the normal python documentation?

Semi-Offtopic: The "search" facility in the Python help has stopped
functioning for me (I'm using XP on this system). No matter what I
search for, I get no results. A week ago, I got a lot of hits for almost
anything I searched for. Anyone seen this behavior, and knows what to do
about it?
 
R

Robert Kern

Jan said:
Robert Kern wrote:
[---]
I need to escape '\n', '"', '[' and ']'. I finally went with a few of
these:
string.replace('\n', '\\n')
string.replace('"', '\\"')
...

I assume that's good enough, but I somehow expected there to exist
some form of "insert your conversion table here" built-in string escaper.

Write a codec.

Is that what string.encode() uses? Some behind-the-scenes "codec"? I
tried searching for it, but could only find UTF/Unicode-related
information. Is it in the normal python documentation?

Semi-Offtopic: The "search" facility in the Python help has stopped
functioning for me (I'm using XP on this system). No matter what I
search for, I get no results. A week ago, I got a lot of hits for almost
anything I searched for. Anyone seen this behavior, and knows what to do
about it?

Use Google:

site:docs.python.org codecs

--
Robert Kern
(e-mail address removed)

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

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
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top