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? 9 1516
Jan Danielsson wrote: 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 rk***@ucsd.edu
"In the fields of hell where the grass grows high
Are the graves of dreams allowed to die."
-- Richard Harter
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!
Jan Danielsson 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!
Hmm... On second thought, I need to escape more characters.
Is there no other way to escape characters in strings?
Jan Danielsson wrote: Jan Danielsson 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!
Hmm... On second thought, I need to escape more characters.
Is there no other way to escape characters in strings?
Which characters?
--
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
Robert Kern wrote:
[---] Hmm... On second thought, I need to escape more characters.
Is there no other way to escape characters in strings?
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.
Jan Danielsson wrote: Robert Kern wrote: [---]
Hmm... On second thought, I need to escape more characters.
Is there no other way to escape characters in strings?
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 rk***@ucsd.edu
"In the fields of hell where the grass grows high
Are the graves of dreams allowed to die."
-- Richard Harter
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?
--
Kind Regards,
Jan Danielsson
Te audire no possum. Musa sapientum fixa est in aure.
Jan Danielsson wrote: 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 rk***@ucsd.edu
"In the fields of hell where the grass grows high
Are the graves of dreams allowed to die."
-- Richard Harter
Jan Danielsson wrote: Robert Kern wrote: [---]
Hmm... On second thought, I need to escape more characters.
Is there no other way to escape characters in strings?
Which characters?
I need to escape '\n', '"', '[' and ']'. I finally went with a few of these: string.replace('\n', '\\n') string.replace('"', '\\"')
You might like this recipe: http://aspn.activestate.com/ASPN/Coo...n/Recipe/81330
Kent This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Ian Rastall |
last post by:
Sorry for the double question. I'm having a terrible time figuring out
how to escape apostrophes in my mySQL database. Perhaps they have to
be escaped in the PHP, using mysql_real_escape_string?
...
|
by: Lisa |
last post by:
I need to apply the HTML formatting tags and the French accented
characters in a XML document. The XML is generated from a database
that has HTML tags and French accented characters in the records....
|
by: Jon Maz |
last post by:
Hi,
I have been getting hopelessly confused with escaping escape characters in
JScript! All I want to do is write a simple funtion:
function DoubleUpBackSlash(inputString)
{
???????
}
|
by: Stan Brown |
last post by:
Can someone tell me if I am misinterpreting the spec?
Specifically, section 2.4 of RFC 2396
<file://localhost/d:/tech/internet/rfc2396.htm> defines "escaped"
characters using the %xx notation...
|
by: agarwalpiyush |
last post by:
Hello,
I am going nuts with trying to get the following to work:
This is what I intend to do: I have a line in /etc/syslog.conf which I
need to delete based on ip-address provided to me in a...
|
by: James Black |
last post by:
I have an application that relies on JSON, and I realized yesterday
that I when the user types at least some of these characters (most
likely all, haven't had time to verify) that the parser has...
|
by: Jon |
last post by:
Hi,
I used XslCompiledTransform with the following Xsl file. The <xsl:text
disable-output-escaping="yes"does not work when using XslCompiledTransform
to do the trnasform (namely the output...
|
by: Taras_96 |
last post by:
Hi everyone,
I'm having a bit of trouble understanding the purpose of escaping
nulls, and the use of addcslashes.
Firstly, the manual states that:
"Strictly speaking, MySQL requires only...
|
by: wannymahoots |
last post by:
optparse seems to be escaping control characters that I pass as
arguments on the command line. Is this a bug? Am I missing
something? Can this be prevented, or worked around?
This behaviour...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
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...
|
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...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
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...
| |