473,414 Members | 1,675 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,414 software developers and data experts.

How to do special encode in string ?

Hi !

I'm hungarian, we use special characters like:
á - a'
õ -o"

etc.

I want to encode this characters to in config file I see these
characters as \nnn format.
And I want to decode it automatically with python.

How to I do it without write complex converter tool ?

Thanx for it:
FT

Example:
Encode("az állam én vagyok") -> "az \xe1llam \xe9n vagyok"

Decode("az \xe1llam \xe9n vagyok") -> "az állam én vagyok"

Jul 18 '05 #1
3 2287
"fo***********@anonym.hu" <fo***********@anonym.hu> wrote in
news:ma***********************************@python. org:
Encode("az állam én vagyok") -> "az \xe1llam \xe9n vagyok"

Decode("az \xe1llam \xe9n vagyok") -> "az állam én vagyok"

s = "az \xe1llam \xe9n vagyok"
print s.decode('latin-1') az állam én vagyok


You want to use unicode strings if you have characters outside the ASCII
range. The decode method on a byte string will let you convert it to a
unicode string, and the encode method will let you convert it back to byte
string.

The tricky bit is that you need to know the correct encoding to use as \xe1
could mean different characters, but in this case it looks as though you
meant latin-1.
Jul 18 '05 #2
On 21 Jun 2004 10:14:57 GMT, Duncan Booth <me@privacy.net> wrote:
"fo***********@anonym.hu" <fo***********@anonym.hu> wrote in
news:ma***********************************@python .org:
Encode("az állam én vagyok") -> "az \xe1llam \xe9n vagyok"

Decode("az \xe1llam \xe9n vagyok") -> "az állam én vagyok"

s = "az \xe1llam \xe9n vagyok"
print s.decode('latin-1')az állam én vagyok


You want to use unicode strings if you have characters outside the ASCII
range. The decode method on a byte string will let you convert it to a
unicode string, and the encode method will let you convert it back to byte
string.

The tricky bit is that you need to know the correct encoding to use as \xe1
could mean different characters, but in this case it looks as though you
meant latin-1.


For Hungarian long umlauts, you'll want to use latin-2 (or iso8859-2).
--
Christopher
Jul 18 '05 #3
> Encode("az llam n vagyok") -> "az \xe1llam \xe9n vagyok"

Decode("az \xe1llam \xe9n vagyok") -> "az llam n vagyok"


The functions you want are str.encode and str.decode:
"az llam n vagyok".encode("string_escape") -> "az \xe1llam \xe9n
vagyok"
"az \xe1llam \xe9n vagyok".decode("string_escape") -> "az llam n
vagyok"

If you choose to use Unicode strings instead, use the "unicode_escape"
codec instead of the "string_escape" codec.

A list of the standard encodings is available at
http://docs.python.org/lib/node127.html if you need with some other
format (rot13 is my personal favourite :P).
Jul 18 '05 #4

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

Similar topics

17
by: Pikkel | last post by:
i'm looking for a way to replace special characters with characters without accents, cedilles, etc.
6
by: Jonas Meurer | last post by:
hello, my script selects a comment saved as VARCHAR in MySQL and displays it inside an html page. the problem is, that the comment contains several special characters, as mysterious utf-8...
1
by: Hardy Wang | last post by:
Hi, I have a XML document, loaded by XmlDocument xDoc = new XmlDocument(); xDoc.Load("my.xml"); xPath = xmlKeyName; XmlNode xNode = xDoc.SelectSingleNode(xPath); Then I will assign some...
1
by: Asha | last post by:
hello how does javascript handle char like &? i have a dynamically build link and javascript cant handle values like &? why is taht?
2
by: Alan Searle | last post by:
I generate XML from an MS-Access 2002 database and find that I can format and display the data no problem with a XSL/HTML template. Perfect! However, then I found that I needed to replace my...
0
by: Luis Corrales | last post by:
Hi all, I have a problem when searching for text with special characters in e-mails in an IMAP server. I'm using imaplib in python 2.4.3 and I can't get this code working: # first connect and...
6
by: 7stud | last post by:
s1 = "hello" s2 = s1.encode("utf-8") s1 = "an accented 'e': \xc3\xa9" s2 = s1.encode("utf-8") The last line produces the error: --- Traceback (most recent call last):
5
by: Sobin Thomas | last post by:
Hi All, I want to pass a string that contains many special characters (: \ . _ etc) to another page in my website through query string. In my project I have a Gridview control ,in which there...
2
by: Art | last post by:
hey, i think this must be really easy: i want to use strings as filenames that include characters that are not allowed in windows filenames. is there a special converter method for this?...
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: 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,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.