how to show Chinese Characters in the value set of a dictionary

Z

zxo102

Hi there,
I have a dictionary with values of Chinses Characters. For
example,
dict = {}
dict['c1']="中国一"
dict['c2']="中国二"
dict.values()
['\xd6\xd0\xb9\xfa\xb6\xfe', '\xd6\xd0\xb9\xfa\xd2\xbb']

Since the result of dict.values will be inserted into web pages and
handled by javascript there, I want to show Chinese Characters
in the list directly like this,

['中国一','中国二']

Anybody knows how to do this? Thank you very much for your help.

Ouyang
 
N

Nikos Kouremenos

Hi there,
I have a dictionary with values of Chinses Characters. For
example,
dict = {}
dict['c1']="中国一"
dict['c2']="中国二"
dict.values()
['\xd6\xd0\xb9\xfa\xb6\xfe', '\xd6\xd0\xb9\xfa\xd2\xbb']

Since the result of dict.values will be inserted into web pages and
handled by javascript there, I want to show Chinese Characters
in the list directly like this,

['中国一','中国二']

Anybody knows how to do this? Thank you very much for your help.

Ouyang
print "['", dict.values()[0], "']", "['", dict.values()[1], "']"
[' 中国二 '] [' 中国一 ']
 
D

Diez B. Roggisch

zxo102 said:
Hi there,
I have a dictionary with values of Chinses Characters. For
example,

dict = {}
dict['c1']="中国一"
dict['c2']="中国二"
dict.values()

['\xd6\xd0\xb9\xfa\xb6\xfe', '\xd6\xd0\xb9\xfa\xd2\xbb']

Since the result of dict.values will be inserted into web pages and
handled by javascript there, I want to show Chinese Characters
in the list directly like this,

['中国一','中国二']

Anybody knows how to do this? Thank you very much for your help.

I can see these chines characters very well - so I don't see why it
won't work putting them into a HTML page. Just nake sure you use the
proper encoding, most probably utf-8.

What put you off probably is the fact that in the interpreter, strings
are printed using their __repr__-method, that puts out those "funny"
hex-characters. But no need to worry there.

Additionally, you should use unicode-objecvts instead of byte-strings

do

# -*- coding: utf-8 -*-
d = dict(c1=u"中国一")

Notice the u in front of the string-literal.

Diez
 
D

Donn Cave

"Diez B. Roggisch said:
What put you off probably is the fact that in the interpreter, strings
are printed using their __repr__-method, that puts out those "funny"
hex-characters. But no need to worry there.

Moreover, the "print" statement also uses repr to convert lists
to strings.

If this generally suits your purposes, and you'd just prefer to avoid
the "escape" translation in strings, then I guess you either have to
write your own repr function for the lists, or for the strings. The
first option should be fairly straightforward. For the second, I'm
thinking of something like this -

class Xtring(types.StringType):
def __init__(self, a):
self.value = a
def __repr__(self):
return '¥'%s¥'' % self.value

dict['c1'] = Xtring('...')

print dict.values()

(Of course you should use unicode instead of string - if you can
figure out how to require the default encoding that supports
your character set. Python has an unfortunate preference for
"ascii" as a default encoding, and that's not likely to be the one
you want if you have any reason to use unicode.

Donn Cave, (e-mail address removed)
 

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

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top