UnicodeEncodeError: 'ascii' codec can't encode character u'\u5728' in position 1

s = "图片picture"
print chardet.detect(s)

for c in s.decode(‘utf-8‘):
print c

UnicodeEncodeError: ‘ascii‘ codec can‘t encode character u‘\u5728‘ in position 1

解决方案:

reload(sys)

sys.setdefaultencoding("utf8")

UnicodeEncodeError: 'ascii' codec can't encode character u'\u5728' in position 1

时间: 2024-11-20 08:10:24

UnicodeEncodeError: 'ascii' codec can't encode character u'\u5728' in position 1的相关文章

UnicodeEncodeError: ‘ascii’ codec can’t encode character u’\u8888′ in position 0: ordinal not in range(168)

在将字符串写入文件时,执行f.write(str),后台总是报错:UnicodeEncodeError: 'ascii' codec can't encode character u'\u8888′ in position 0: ordinal not in range(168),即ascii码无法被转换成unicode码.在仔细推敲后发现,我所使用的python2.7,默认编码是ascii格式.可以使用如下语句查看Python默认编码格式: >>> import sys >>

UnicodeEncodeError: 'ascii' codec can't encode character u'\u5929' in position 2: ordinal not in range(128)

UnicodeEncodeError: 'ascii' codec can't encode character u'\u5929' in position 2: ordinal not in range(128) UnicodeEncodeError: 'latin-1' codec can't encode character u'\u65e0' in position 599: ordinal not in range(256) UnicodeEncodeError: 'ascii' co

UnicodeEncodeError: 'ascii' codec can't encode character u'\u65e0' in position 1: ordinal not in range(128)

UnicodeEncodeError: 'ascii' codec can't encode character u'\u65e0' in position 1: ordinal not in range(128) 上述问题解决办法 修改tool.py s = json.dumps(obj, sort_keys=True, indent=4, ensure_ascii=False)        outfile.write(codecs.encode(s, 'utf-8')) 修改后 修改前 U

UnicodeEncodeError: 'ascii' codec can't encode character...的解决方法

在python2.7下,因为想从数据库中读出来分类名进行写入到文件,提示 Traceback (most recent call last): File "test.py", line 28, in <module> fp.write("%d:%s\r\n"%(sClassid,sClassName)) UnicodeEncodeError: 'ascii' codec can't encode character u'\uff08' in positi

Python 错误 UnicodeEncodeError: &#39;ascii&#39; codec can&#39;t encode character 的解决方案

一.使用workbook.get_sheet_by_name(name) 获取excel一个工作表时,发生字符集解析的错误. 网上搜集解决方案为添加以下三句: import sys reload(sys) sys.setdefaultencoding("utf8") Python 错误 UnicodeEncodeError: 'ascii' codec can't encode character 的解决方案

UnicodeEncodeError: ‘gbk’ codec can’t encode character u’\u200e’ in position 43: illegal multibyte sequence

[问题] python中已获取网页: http://blog.csdn.net/hfahe/article/details/5494895 的html源码,其时UTF-8编码的. 提取出其标题部分: <span class="link_title"><a href="/hfahe/article/details/5494895"> 在2008 Beijing Perl 大会的演讲-使用Mason开发高性能的Web站点‎ </a>&

解决“UnicodeEncodeError: &#39;gbk&#39; codec can&#39;t encode character u&#39;\xa9&#39; in position 24051: illegal multibyte sequence”错误

今天我在爬取一个网页时出现了下面这个错误: UnicodeEncodeError: 'gbk' codec can't encode character u'\xa9' in position 24051: illegal multibyte sequence 从这个错误来看应该是编码时出错了,于是我在网上查了些资料,找到了解决方法: source_code.encode('GB18030') 解释:GB18030是GBK的父集,所以能兼容GBK不能编码的字符. 解决"UnicodeEncode

解决windows环境中python2.7&#39;ascii&#39; codec can&#39;t encode character

在python2.7下,将字符串写入到文件时会出现"UnicodeEncodeError: 'ascii' codec can't encode character u'\xa0' in position"的错误,原因是由于python基于ASCII处理字符的,当出现不属于ASCII的字符时,会出现错误信息. 原因是pip安装python包会加载我的用户目录,我的用户目录恰好是中文的,ascii不能编码.解决办法是: python目录 Python27\Lib\site-package

Python读取中文txt文件错误:UnicodeEncodeError: &#39;gbk&#39; codec can&#39;t encode character

1 with open(file,'r') as f: 2 line=f.readline() 3 i=1 4 while line: 5 line=line.decode('utf-8') 6 print str(i)+": "+line7 line=f.readline() 8 i=i+1 用以上代码读取一个包含中文的txt文件时,在正确地读取并打印了六百多行之后,print str(i)+": "+line这一行报错: UnicodeEncodeError: