首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在python中解码以下urls

在python中解码以下urls
EN

Stack Overflow用户
提问于 2014-11-06 08:59:47
回答 1查看 64关注 0票数 0

我有一个这样的URL:

代码语言:javascript
复制
http://idebate.org/debatabase/debates/constitutional-governance/house-supports-dalai-lama%E2%80%99s-%E2%80%98third-way%E2%80%99-tibet

然后我使用python中的以下脚本来解码这个url:

代码语言:javascript
复制
full_href = urllib.unquote(full_href.encode('ascii')).decode('utf-8')

但是,我得到的错误如下:

代码语言:javascript
复制
UnicodeEncodeError: 'ascii' codec can't encode character u'\u2019' in position 89: ordinal not in range(128)

当尝试写入文件时

EN

回答 1

Stack Overflow用户

发布于 2014-11-06 17:46:35

正如@KevinJ.Chase所指出的,您很可能正在尝试使用不兼容ascii格式的字符串写入文件。您可以更改写文件编码,或将full_href编码为ascii,如下所示:

代码语言:javascript
复制
# don't decode again to utf-8
full_href = urllib.unquote(url.encode('ascii'))
... then write to your file stream

或,

代码语言:javascript
复制
...
# encode your your to compatible encoding on write, ie. utf-8
with open('yourfilenamehere', 'w') as f:
    f.write(full_href.encode('utf-8'))
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26770031

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档