- 论坛徽章:
- 0
|
本帖最后由 wzhuzhu 于 2010-07-30 09:59 编辑
大家好,我现在用Python写网络程序,在处理传送过来的中文时存在一个问题,请大家指教:
客户端传输过来的字节: \x4e\x00\x4e\x8c\x4e\x09
其实真好对应中文的: 一二三的utf-8的编码。
接收过来的字符串怎么处理都不能得到正确的中文:
- >> test_str = '\x4e\x00\x4e\x8c\x4e\x09'
- >>> test_str.decode('utf8')
- Traceback (most recent call last):
- File "<stdin>", line 1, in <module>
- File "/usr/lib/python2.6/encodings/utf_8.py", line 16, in decode
- return codecs.utf_8_decode(input, errors, True)
- UnicodeDecodeError: 'utf8' codec can't decode byte 0x8c in position 3: unexpected code byte
- >>> test_str.decode('utf16')
- u'N\u8c4e\u094e'
- >>>test_str = u"一二三"
- >>> test_str
- u'\u4e00\u4e8c\u4e09'
复制代码 请问我要如何操作才能得到正确的中文。
谢谢! |
|