0
我使用urllib.request.urlopen
从Web API获取* .srt文件。 (相关)代码(Python 3.x):“'charmap'编解码器无法编码字符”(Http请求)
with urllib.request.urlopen(req) as response:
result = response.read().decode('utf-8')
print(result)
with open(subpath, 'w') as file:
file.write(result)
file.close()
这工作正常,除了一些文件。对于某些文件,我收到以下错误: UnicodeEncodeError: 'charmap' codec can't encode character '\u266a' in position 37983: character maps to <undefined>
(\ u266a是四分音符符号。)
如何解决这个问题?我可以从.read()返回的字节对象中过滤这个字符吗?或者我可以编码错误被忽略?提前致谢。
另外,请注意我没有找到有关话题无数”使用.decode ......不能编码字符...'-错误,但是,在大多数情况下(‘UTF-8’)是解决办法。