Python读取字符串,带'&#x';然后打印出来

Python读取字符串,带'&#x';然后打印出来,python,unicode,iso-8859-1,Python,Unicode,Iso 8859 1,我正在用Python3.5阅读一个json文件。在这个文件中,它有像“í”这样的字符。我想用那种格式打印出来。如何使下面的代码正确打印字符 t = 'í' print(t) Traceback (most recent call last): File "test.py", line 15, in <module> print(t) UnicodeEncodeError: 'ascii' codec can't encode character '\xed' in po

我正在用Python3.5阅读一个json文件。在这个文件中,它有像“í”这样的字符。我想用那种格式打印出来。如何使下面的代码正确打印字符

t = 'í'
print(t)

Traceback (most recent call last):
  File "test.py", line 15, in <module>
    print(t)
UnicodeEncodeError: 'ascii' codec can't encode character '\xed' in position 0: ordinal not in range(128)
t='í'
打印(t)
回溯(最近一次呼叫最后一次):
文件“test.py”,第15行,在
打印(t)
UnicodeEncodeError:“ascii”编解码器无法对位置0中的字符“\x”进行编码:序号不在范围内(128)
使用unicode格式

t = u'i'
print(t)
必须在字符“i”之前添加
u
,以便python将其理解为unicode。

尝试添加
#-*-编码:iso-8859-15-*-
作为源文件的第一行或第二行。

尝试以下操作:

print(t.decode("utf-8"))
试试这个-

# -*- coding: utf-8 -*-
from __future__ import unicode_literals
t = 'í'
print(t.encode("ascii" , "ignore"))

可能是复制品,我无法复制,它对我来说很好。您确定使用的是Python 3吗?在这种情况下,只有Python2应该有这个错误。这就是在3.6.1中使用Python2Print(sys.version\u info)结果运行它时所得到的错误。json文件是使用:with open('ep\u meps\u current.json','r',encoding='utf-8')作为fp:meps=json.load(fp)打开的,在键入文本的情况下,这是正确的。OP的用例是如何从文件中读取字符串的?我们可以在导入json文件Python 2或3???@bigbounty 3.6期间指定编码。1@aryamccarthyjson文件是使用:with open('ep_meps_current.json','r',encoding='utf-8')作为fp:meps=json.load(fp)打开的