简体   繁体   English

在python中将特殊字符转换为原始形式

[英]Convert special characters in to original form in python

Suppose i had a string for example 假设我有一个字符串例如

>>>  stri = "日本"
>>>  res = stri
>>>  res
'\xe6\x97\xa5\xe6\x9c\xac'

Now i want to convert the result in res back to the form in "日本" . 现在我想将res的结果转换回"日本"的表单。 How can i do this thanks in advacnce 我怎么能这样做,谢谢你的建议

(Assuming that you're using Python 2.x on a UTF-8 console): (假设您在UTF-8控制台上使用Python 2.x):

Nothing has been converted, and there is no need to convert anything back; 没有任何东西被转换,也没有必要改变任何东西; what you're seeing is the internal representation of the string. 你所看到的是字符串的内部表示。 Try print ing it. 尝试print它。

>>>  stri = "日本"
>>>  stri
'\xe6\x97\xa5\xe6\x9c\xac'
>>> print(stri)
日本

To clarify: 澄清:

If you enter the name of a Python variable in the console, the console will print the repr of that variable. 如果在控制台中输入Python变量的名称,控制台将打印该变量的repr If you want to print the variable in human-readable form, use print instead. 如果要以人类可读的形式打印变量,请使用print There is no difference in the way the variable is stored, therefore there's nothing to convert. 变量的存储方式没有区别,因此无需转换。

That is the expected behaviour - console doesn't print variable in unicode. 这是预期的行为 - 控制台不会在unicode中打印变量。 If you actually print it out you'll see that the correct chars are still there. 如果你实际打印出来,你会发现正确的字符仍在那里。 The console automatically uses repr on every variable before printing it out. 在打印出来之前,控制台会自动对每个变量使用repr。 You can verify that yourself by doing print(repr...)) like the example below: 您可以通过执行print(repr ...)来验证自己,如下例所示:

>>> stri = "日本"
>>> stri
'\xe6\x97\xa5\xe6\x9c\xac'
>>> print stri
日本
>>> print repr(stri)
'\xe6\x97\xa5\xe6\x9c\xac'
>>> 

Like Tim said, the characters haven't been converted. 像蒂姆说的那样,角色还没有被转换。

This article should help you understand what's happening 本文应该可以帮助您了解正在发生的事情

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

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