By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,067 Members | 956 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,067 IT Pros & Developers. It's quick & easy.

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

P: n/a
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

Jan 1 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
On Sun, 01 Jan 2006 07:35:31 -0800, zxo102 wrote:

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], "']"

[' *国二 '] [' *国一 ']

Jan 1 '06 #2

P: n/a
zxo102 schrieb:
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
Jan 1 '06 #3

P: n/a
In article <41*************@uni-berlin.de>,
"Diez B. Roggisch" <de***@nospam.web.de> wrote:
....
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, do**@u.washington.edu
Jan 3 '06 #4

P: n/a
On 1 Jan 2006 07:35:31 -0800, rumours say that "zxo102" <zx****@gmail.com>
might have written:
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,

['???','???']


Diez's instructions were useful to you, however for quick previewing check
this recipe, it might be useful:

http://aspn.activestate.com/ASPN/Coo.../Recipe/439148
--
TZOTZIOY, I speak England very best.
"Dear Paul,
please stop spamming us."
The Corinthians
Jan 13 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.