473,416 Members | 1,968 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,416 software developers and data experts.

Translating escaped characters

How do you best go about translating characters like '\\n' to '\n'?
This is for a configuration file parser, where the "backslash
convention" is supported.

The naive approach -- re.sub('\\\\(.)', '\\\1', s) -- doesn't work, of
course. The best I've come up with so far is a special case for every
character to be translated. There must be an easier way?

--
Henrik S. Hansen http://freecode.dk/~hsh/
Computer science (software engineering) student
Free Software Foundation member #1702 (http://member.fsf.org)
Jul 18 '05 #1
2 1427
Henrik S. Hansen <hsh <at> freecode.dk> writes:

How do you best go about translating characters like '\\n' to '\n'?


Is this what you're looking for?
'\\n'.decode('string_escape') '\n' '\n'.encode('string_escape')

'\\n'

There's some info about it here:

http://docs.python.org/lib/node127.html

and searching Google Groups shows up a few things too:

http://groups.google.com/groups?q=escape+sequences&
meta=group%3Dcomp.lang.python.*

(Sorry, the URL wrapped.)

Steve

Jul 18 '05 #2
Steven Bethard <st************@gmail.com> writes:
How do you best go about translating characters like '\\n' to '\n'?


Is this what you're looking for?
'\\n'.decode('string_escape') '\n' '\n'.encode('string_escape')

'\\n'


Precisely, thank you.

--
Henrik S. Hansen http://freecode.dk/~hsh/
Computer science (software engineering) student
Free Software Foundation member #1702 (http://member.fsf.org)
Jul 18 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Jay | last post by:
Is there a definitive list of characters that must be escaped in order to insert them into a text field in mysql? TIA, Jay
2
by: Don Robertson | last post by:
Greetings, I need to be able to output text with some characters escaped - eg change a &#xA; to \n I am at a loss as how to do this. I am trying translate($text,'&#xA;','\n') and
5
by: Jonathan Seidner | last post by:
Hi, I have a program which contains a textbox, in this text box the user types this string: "\ub7c6\ub9d1\ubbd9\ubdc8\ubfcb\uc1b2\uc3a7\uc586\uc7a9\uc9b0\ucbfb" this is NOT a unicode string...
0
by: DaveS | last post by:
I have a web service that returns one parameter which is defined as a string. In this string is returned an XML document (due to the fact that I have a large amount of data to pass back to the...
13
by: Oxns | last post by:
Hi, Can anyone point me at the class to convert a string so that it displays escaped chars as \r, \n etc. please Its done in the 2005 debugger so I hope its available as a class ??. Thanks...
12
by: Torsten Bronger | last post by:
Hallöchen! I need some help with finding matches in a string that has some characters which are marked as escaped (in a separate list of indices). Escaped means that they must not be part of...
1
by: bwilcoxis | last post by:
I have an externally generated xml file that correctly escaping characters such as apostrophes and quotes and rendering them as &amp;#8217; and &amp;#39; the xml file has the following...
9
by: Michael Goerz | last post by:
Hi, I am writing unicode stings into a special text file that requires to have non-ascii characters as as octal-escaped UTF-8 codes. For example, the letter "Ã" (latin capital I with acute,...
0
by: hash4sp | last post by:
Hello ! I have a problem with the escaped charactes which I am sending to an aspx page. This is javascript escaped characters %u0646%u062A%u0627%u0626%u062C%20%u0627%u0644%u062 8%u062D%u062B ...
0
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
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...
0
tracyyun
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.