Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
String Python2.7:如何获得正确的汉字?_String_Python 2.7_Unicode - Fatal编程技术网

String Python2.7:如何获得正确的汉字?

String Python2.7:如何获得正确的汉字?,string,python-2.7,unicode,String,Python 2.7,Unicode,我在XML文件中打开中文字符串时遇到问题。 Python 2.7.5 (default, May 15 2013, 22:44:16) [MSC v.1500 64 bit (AMD64)] on win32 Type "copyright", "credits" or "license()" for more information. >>> from io import open >>> file = open(u'/senti/cet_2.xml', e

我在XML文件中打开中文字符串时遇到问题。

Python 2.7.5 (default, May 15 2013, 22:44:16) [MSC v.1500 64 bit (AMD64)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> from io import open
>>> file = open(u'/senti/cet_2.xml', encoding = u'utf-8')
>>> contents = file.read()
>>> contents
u'<?xml version="1.0" encoding="UTF-8" standalone="yes"?>\n<document>\n
<Topic>\u5584\u826f \u4e30\u5bcc \u9ad8\u8d35</Topic>\n    <title T="\u5584\u826f\uff0c \u4e30\u5bcc\uff0c\u9ad8\u8d35">\n
win32上的Python 2.7.5(默认,2013年5月15日,22:44:16)[MSC v.1500 64位(AMD64)] 有关详细信息,请键入“copyright”、“credits”或“license()”。 >>>从io导入打开 >>>文件=open(u'/senti/cet_2.xml',编码=u'utf-8') >>>contents=file.read() >>>内容 u'\n\n \u5584\u826f\u4e30\u5bcc\u9ad8\u8d35\n\n 但是,我对Python 3.3中的相同代码没有问题

Python 3.3.2 (v3.3.2:d047928ae3f6, May 16 2013, 00:06:53) [MSC v.1600 64 bit (AMD64)]  on win32
>>> file = open('/Senti/cet_2.xml', encoding = 'utf-8')
>>> contents = file.read()
>>> contents
'<?xml version="1.0" encoding="UTF-8" standalone="yes"?>\n<document>\n
<Topic>善良 丰富 高贵</Topic>\n    <title T="善良,丰富,高贵">\n        
win32上的Python 3.3.2(v3.3.2:d047928ae3f6,2013年5月16日,00:06:53)[MSC v.1600 64位(AMD64)] >>>文件=打开('/Senti/cet_2.xml',编码='utf-8') >>>contents=file.read() >>>内容 “\n\n 善良 丰富 高贵\n\n
如何在Python 2.7中获得正确的字符串?

它已经是正确的中文字符了<代码>打印内容将显示所需内容。导入编解码器。。。file=codecs.open('/Senti/cet_2.xml',r',utf-8')谢谢,问题解决了。但为什么会这样?我仍然有其他代码的问题。希望我能理解这背后发生了什么…@falsetru谢谢,问题解决了。但为什么会这样?我仍然有其他代码的问题。希望我能理解这背后发生了什么。