- 論壇徽章:
- 0
|
本帖最后由 wzhuzhu 于 2010-07-30 09:59 編輯
大家好,我現(xiàn)在用Python寫網(wǎng)絡(luò)程序,在處理傳送過(guò)來(lái)的中文時(shí)存在一個(gè)問(wèn)題,請(qǐng)大家指教:
客戶端傳輸過(guò)來(lái)的字節(jié): \x4e\x00\x4e\x8c\x4e\x09
其實(shí)真好對(duì)應(yīng)中文的: 一二三的utf-8的編碼。
接收過(guò)來(lái)的字符串怎么處理都不能得到正確的中文:
- >> 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'
復(fù)制代碼 請(qǐng)問(wèn)我要如何操作才能得到正確的中文。
謝謝! |
|