简体   繁体   English

如何使用python将utf-8字符串转换为big5?

[英]How to convert utf-8 string to big5 with python?

I use Python 2.6.6 My locale is ('en_US', 'UTF8') 我使用Python 2.6.6,我的语言环境是('en_US','UTF8')

I try many ways to convert utf-8 string to big5, but it can't work. 我尝试了多种方法将utf-8字符串转换为big5,但无法正常工作。 If you know how to do that, please give me some advice, thanks a lot. 如果您知道该怎么做,请给我一些建议,非常感谢。


A chinese word called '單車', it mean 'bicycle' 中文单词“单车”,意思是“自行车”

It's unicode is \單\車 它的Unicode是\\ u55ae \\ u8eca

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

I know the str_a can be work, but I want to convert str_b to big5, too. 我知道str_a可以工作,但是我也想将str_b转换为big5。

I try out decode, encode, unicode, but it still can't work. 我尝试解码,编码和unicode,但仍然无法正常工作。

Have any good idea? 有什么好主意吗? Thanks. 谢谢。

str_b is a sequence of bytes: str_b是字节序列:

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

The backslash and u and so forth all are just separate characters. 反斜杠和u等都是单独的字符。 Compare that to sequence of unicode code points in the unicode object str_a : 将其与unicode对象str_a的unicode代码点序列进行比较:

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

To convert the mal-formed string str_b to unicode decode with unicode-escape : 要将格式str_b字符串str_b转换为unicode-escape进行unicode解码,请执行以下操作:

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

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

You should be able to do this: 您应该可以执行以下操作:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM