UnicodeEncodeError: 'ascii' 编解码器无法对特殊名称的字符进行编码 [英] UnicodeEncodeError: 'ascii' codec can't encode character at special name

查看:42
本文介绍了UnicodeEncodeError: 'ascii' 编解码器无法对特殊名称的字符进行编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 python (ver 2.7) 脚本运行良好,可以从本地 html 文件中获取一些公司名称,但是当涉及到某个特定的国家/地区名称时,它会出现此错误UnicodeEncodeError: 'ascii' codec can't encode character"

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"

出现这个公司名称时特别报错

Specially getting error when this company name comes

公司名称:Kühlfix Kälteanlagen Ing.Gerhard Doczekal &有限公司

无法处理链接

Traceback (most recent call last): 
  File "C:Python27Process2.py", line 261, in <module>
    flog.write("
Company Name: "+str(pCompanyName))
UnicodeEncodeError: 'ascii' codec can't encode character u'xfc' in position 9: ordinal not in range(128)

在这行代码中给出错误:

if companyAlreadyKnown == 0:
   for hit in soup2.findAll("h1"):
       print "Company Name: "+hit.text
       pCompanyName = hit.text
       flog.write("
Company Name: "+str(pCompanyName))
       companyObj.setCompanyName(pCompanyName)

推荐答案

尝试在脚本开始时将系统默认编码设置为 utf-8,以便所有字符串都使用该编码.

Try setting the system default encoding as utf-8 at the start of the script, so that all strings are encoded using that.

示例 -

import sys
reload(sys)
sys.setdefaultencoding('utf-8')

上面应该设置默认编码为 utf-8 .

The above should set the default encoding as utf-8 .

这篇关于UnicodeEncodeError: 'ascii' 编解码器无法对特殊名称的字符进行编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆