我有一个这样的URL:
http://idebate.org/debatabase/debates/constitutional-governance/house-supports-dalai-lama%E2%80%99s-%E2%80%98third-way%E2%80%99-tibet然后我使用python中的以下脚本来解码这个url:
full_href = urllib.unquote(full_href.encode('ascii')).decode('utf-8')但是,我得到的错误如下:
UnicodeEncodeError: 'ascii' codec can't encode character u'\u2019' in position 89: ordinal not in range(128)当尝试写入文件时
发布于 2014-11-06 17:46:35
正如@KevinJ.Chase所指出的,您很可能正在尝试使用不兼容ascii格式的字符串写入文件。您可以更改写文件编码,或将full_href编码为ascii,如下所示:
# don't decode again to utf-8
full_href = urllib.unquote(url.encode('ascii'))
... then write to your file stream或,
...
# encode your your to compatible encoding on write, ie. utf-8
with open('yourfilenamehere', 'w') as f:
f.write(full_href.encode('utf-8'))https://stackoverflow.com/questions/26770031
复制相似问题