UNI $ C $岑codeError:“ASCII”codeC不能在特别的名字EN code字 [英] UnicodeEncodeError: 'ascii' codec can't encode character at special name

查看:264
本文介绍了UNI $ C $岑codeError:“ASCII”codeC不能在特别的名字EN code字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Python(版本2.7)脚本运行良好获得本地HTML文件的一些公司的名字,但是当涉及到​​一些具体的国家名称,它给这个错误的Uni $ C $岑codeError:ASCII codeC无法连接code字符

特别得到错误时,这家公司的名字来自

公司名称:KühlfixKälteanlagenIng.Gerhard Doczekal&安培; Co. KG的

的链接,不能被处理

 回溯(最后最近一次调用):
  文件C:\\ Python27 \\ Process2.py,261线,上述<&模块GT;
    flog.write(\\ n公司名称:+ STR(pCompanyName))
UNI $ C $岑codeError:ASCIIcodeC无法连接code字符U'\\ XFC'位置9:序数不在范围内(128)

错误给在这一行code的:

 如果companyAlreadyKnown == 0:
   对于砸在soup2.findAll(H1):
       打印公司名称:+ hit.text
       pCompanyName = hit.text
       flog.write(\\ n公司名称:+ STR(pCompanyName))
       companyObj.setCompanyName(pCompanyName)


解决方案

尝试在脚本的开始设置系统默认的编码为 UTF-8 ,使所有字符串都设有codeD使用。

示例 -

 进口SYS
重装(SYS)
sys.setdefaultencoding函数(UTF-8)

以上应该设置默认的编码为 UTF-8

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

Company Name: 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)

解决方案

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

Example -

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

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

这篇关于UNI $ C $岑codeError:“ASCII”codeC不能在特别的名字EN code字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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