Python UnicodeEncodeError: 'ascii' 编解码器无法对特殊名称的字符进行编码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31137552/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
UnicodeEncodeError: 'ascii' codec can't encode character at special name
提问by rhb1
My python (ver 2.7) script is running well to get some company name from local html files but when it comes to some specific country name, it gives this error "UnicodeEncodeError: 'ascii' codec can't encode character"
我的 python (ver 2.7) 脚本运行良好,可以从本地 html 文件中获取一些公司名称,但是当涉及到某个特定的国家/地区名称时,它会出现此错误“UnicodeEncodeError: 'ascii' codec can't encode character”
Specially getting error when this company name comes
出现这个公司名称时特别报错
Company Name: Kühlfix K?lteanlagen Ing.Gerhard Doczekal & Co. KG
公司名称:Kühlfix K?lteanlagen Ing.Gerhard Doczekal & Co. KG
The link cannot be processed
链接无法处理
Traceback (most recent call last):
File "C:\Python27\Process2.py", line 261, in <module>
flog.write("\nCompany Name: "+str(pCompanyName))
UnicodeEncodeError: 'ascii' codec can't encode character u'\xfc' in position 9: ordinal not in range(128)
Error gives in this line of code:
在这行代码中给出了错误:
if companyAlreadyKnown == 0:
for hit in soup2.findAll("h1"):
print "Company Name: "+hit.text
pCompanyName = hit.text
flog.write("\nCompany Name: "+str(pCompanyName))
companyObj.setCompanyName(pCompanyName)
采纳答案by Anand S Kumar
Try setting the system default encoding as utf-8
at the start of the script, so that all strings are encoded using that.
尝试utf-8
在脚本开始时设置系统默认编码,以便使用该编码对所有字符串进行编码。
Example -
例子 -
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
The above should set the default encoding as utf-8
.
以上应将默认编码设置为utf-8
.
回答by David Cullen
You really want to do this
你真的想这样做
flog.write("\nCompany Name: "+ pCompanyName.encode('utf-8'))
This is the "encode late" strategy described in this unicode presentation(slides 32 through 35).
这是本unicode 演示文稿(幻灯片 32 到 35)中描述的“延迟编码”策略。