2011-01-05 532 views
1

我使用Python 2.6.6 我的語言環境是(「EN_US」,「utf-8」)如何使用python將utf-8字符串轉換爲big5?

我嘗試了很多方式爲UTF-8字符串轉換爲BIG5,但它不能正常工作。 如果你知道如何做到這一點,請給我一些建議,非常感謝。


一箇中國詞叫 '單車',它的意思是 '自行車'

它的unicode是\ u55ae \ u8eca

str_a = u'\u55ae\u8eca' 
str_b = '\u55ae\u8eca' 
print str_a # output '單車' 
print str_b # output '\u55ae\u8eca' 

我知道str_a可以工作,但我想將str_b轉換爲big5也是如此。

我嘗試解碼,編碼,unicode,但它仍然無法正常工作。

有什麼好主意嗎?謝謝。

+0

「我嘗試解碼,編碼,統一,但它仍然無法工作。「請張貼您嘗試的代碼和您遇到的問題。您不會將UTF-8轉換爲Big-5。您將UTF-8解碼爲Unicode。然後你將Unicode編碼成Big-5。請發佈解碼和編碼您嘗試和您與他們的問題。 – 2011-01-05 11:38:49

+0

感謝您的建議,我會注意到這一點。 – 2011-01-06 03:48:19

回答

5

str_b是一個字節序列:

In [19]: list(str_b) 
Out[19]: ['\\', 'u', '5', '5', 'a', 'e', '\\', 'u', '8', 'e', 'c', 'a'] 

反斜線和u等等一切都只是單獨的字符。與此相比,中Unicode代碼點序列中的Unicode對象str_a

In [24]: list(str_a) 
Out[24]: [u'\u55ae', u'\u8eca'] 

到惡意形成的字符串str_b轉換爲Unicode解碼與unicode-escape

In [20]: str_b.decode('unicode-escape') 
Out[20]: u'\u55ae\u8eca' 

In [21]: print(str_b.decode('unicode-escape')) 
單車 
+0

太棒了!這是我想要的答案。非常感謝你。 – 2011-01-06 03:08:09

3

你應該能夠做到這一點:

str_a = u'\u55ae\u8eca' 
str_b = str_a.encode('big5') 
print str_a 
print str_b.decode('big5') 
+0

加上'repr()'得到他想要的輸出。 – marcog 2011-01-05 11:39:40

相關問題