[英]Cannot convert unicode to utf-8 in python
It must be a trivial task but I can't handle it. 这一定是一项琐碎的任务,但我无法处理。 I have json that looks like this.
我有看起来像这样的json。
{'
city': u'\\u0410\\u0431\\u0430\\u043a\\u0430\\u043d',
'language':{
u'\\u0410\\u043d\\u0433\\u043b\\u0438\\u0439\\u0441\\u043a\\u0438\\u0439': 5608,
u'\\u0418\\u0442\\u0430\\u043b\\u044c\\u044f\\u043d\\u0441\\u043a\\u0438\\u0439': 98
}
},
I'm trying to convert the unicode strings into utf-8. 我正在尝试将unicode字符串转换为utf-8。
string=u'\u0410\u0431\u0430\u043a\u0430\u043d'
string.encode('utf-8')
I've got 我有
'\xd0\x90\xd0\xb1\xd0\xb0\xd0\xba\xd0\xb0\xd0\xbd'
Instead of: 代替:
u'Абакан'
What am I doing wrong? 我究竟做错了什么?
What am I doing wrong?
我究竟做错了什么?
Not printing it. 不打印。
When you just evaluate a string in Python REPL, you will get its repr
. 当您仅在Python REPL中评估字符串时,将获得其
repr
。 This is '\\xd0\\x90\\xd0\\xb1\\xd0\\xb0\\xd0\\xba\\xd0\\xb0\\xd0\\xbd'
. 这是
'\\xd0\\x90\\xd0\\xb1\\xd0\\xb0\\xd0\\xba\\xd0\\xb0\\xd0\\xbd'
。 When you print it, you will get Абакан
. 打印时,您会得到
Абакан
。
print(string.encode('utf-8'))
As @Amadan said, you just need to print your string. 正如@Amadan所说,您只需要打印字符串即可。
But why printing string resolves the problem? 但是,为什么打印字符串可以解决问题?
The answer is that if you type string
+ Enter this will lead to display the representation of repr()
the of the object string
; 答案是,如果您键入
string
+ Enter,这将导致显示对象string
的repr()
的表示形式。 while running print string (or print (string) in Python 3.x) you will get a human readable string representation - str()
- of string
. 在运行(在Python 3.x或打印(串))打印字符串,你会得到一个人类可读的字符串表示-
str()
-的string
。
>>> converted = string.encode('utf8')
>>> converted
'\xd0\x90\xd0\xb1\xd0\xb0\xd0\xba\xd0\xb0\xd0\xbd'
>>> print converted
Абакан
>>> print repr(converted)
'\xd0\x90\xd0\xb1\xd0\xb0\xd0\xba\xd0\xb0\xd0\xbd'
>>> print str(converted)
Абакан
>>>
Further reading: Difference between __str__ and __repr__ in Python 进一步阅读: Python中__str__和__repr__之间的区别
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.