避免UnicodeDecodeError异常Python

避免UnicodeDecodeError异常Python,python,Python,在python中,我使用html模板来显示steam播放器的信息 模板为: '''<td> <div> Name: %s<br> Hours: %s<br> <a href="http://steamcommunity.com/profiles/%s" target="_blank">Steam Profile</a> <br> </div> </td>''' “” 名称:%s 小时数:

在python中,我使用html模板来显示steam播放器的信息

模板为:

'''<td>
<div>
Name: %s<br>
Hours: %s<br>
<a href="http://steamcommunity.com/profiles/%s" target="_blank">Steam Profile</a> <br>
</div>
</td>'''
“”
名称:%s
小时数:%s

'''
所以我有了模板%(personame,tf2Hours,id64)

稍后,该模板将保存到html文件中

有时它会返回UnicodeDecodeError,因为PersonalName可能包含奇怪的字符

在最终的html文件中仍然有正确的字符时,有没有办法避免这种情况

编辑:

错误的原因是非unicode字符

使用unicode(personName,errors='ignore')解决了这个问题。

试试:

 u'UnicodeTextHereaあä'.encode('ascii', 'ignore')
这将忽略无法转换为ascii的unicode字符

这里有几个我刚刚尝试过的例子

>>> x = 'Hello world!'
>>> y = 'notあä ascii'
>>> x.encode('ascii', 'ignore')
b'Hello world!'
>>> y.encode('ascii', 'ignore')
b'not ascii'
如您所见,它删除了所有非ascii字符的痕迹


或者,您可以告诉解释器您正计划读取unicode值。例如(来自),


这将解释并允许您按原样读取unicode。

'Dawn–™ˆOf–™ˆ™ˆdash''encode('ascii','ignore')返回一个错误。我完全不知道为什么,这真的很令人沮丧D:这个怎么样<代码>行。解码('utf-8','ignore')。编码('ascii','ignore')
with open('unicode.txt', encoding='utf-8') as f:
    for line in f:
        print(repr(line))