python错误之UnicodeEncodeError: 'ascii' codec can't encode characters in position 7-8: ordinal not in range(128)

# coding = ascii

import jsonimport pickleimport sysimport os
  • decode()和encode方法中第一个参数为编码格式,第二个为出现无法转换时使用何种处理方式(ignore:忽略,无法转换则为空;replace:用?代替)
  由于在python3中字符编码默认为unicode,所有直接调用encode方法实现字符编码的转换,由于asscii字符中本来就不包含中文字符,所以decode为他

  会造成报错,是用replace后虽然能比避免报错,但是会再最终解码后造成中文为??的结果

# 字符转码实现test = "你好"#获得当前文件编码格式print(sys.getdefaultencoding())print(test.encode("utf-8").decode("ascii","replace").encode("ascii","replace").decode("utf-8"))exec(test)

python错误之UnicodeEncodeError: 'ascii' codec can't encode characters in position 7-8: ordinal not in range(128)

时间: 2024-10-06 02:10:52

python错误之UnicodeEncodeError: 'ascii' codec can't encode characters in position 7-8: ordinal not in range(128)的相关文章

UnicodeEncodeError: 'ascii' codec can't encode characters in position 2-5: ordin al not in range(128)——解决方案备注

在vim中使用ycm插件时,偶尔会出现: “UnicodeEncodeError: 'ascii' codec can't encode characters in position 2-5: ordin al not in range(128)” 自行百度时,发现该提示和python有关,联想到ycm依赖python,故而尝试: 在 /etc/python2.x/sitecustomize.py文件开始处插入如下代码: import sys reload(sys) sys.setdefault

python报错“UnicodeEncodeError: 'ascii' codec can't encode characters in position 22-26: ordinal not in range(128)”问题解决

方案是在python的Lib\site-packages文件夹下新建一个sitecustomize.py,内容为: Python代码  : # encoding=utf8 import sys reload(sys) sys.setdefaultencoding('utf8') 此时重启python解释器,执行sys.getdefaultencoding(),发现编码已经被设置为utf8的了,多次重启之后,效果相同,这是因为系统在python启动的时候,自行调用该文件,设置系统的默认编码,而不需

python报错 UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-3: ordinal not in range(128)

报错:UnicodeEncodeError: ‘ascii’ codec can’t encode characters in position 0-3: ordinal not in range(128) 解决方法,在文件开头添加: import sys; reload(sys); sys.setdefaultencoding('utf8') 问题是在练习爬虫的时候,抓取api接口的json数据转换时出现的,后面参考python格式化输出文章做了一些排版处理,如下 python报错 Unico

Mac sublime 编译Python UnicodeEncodeError: 'ascii' codec can't encode characters in position 6-8: ordinal not in range(128)

刚学Python,想打印个"hello 张林峰",代码如下: #!/usr/bin/env python3 # -*- coding: utf-8 -*- print('hello 张林峰') 用sublime运行一下,竟然报错??? Traceback (most recent call last): File "/Users/zhanglinfeng/Documents/Python/\u7ec3\u4e60/\u5b57\u7b26\u4e32\u7f16\u7801\

Python UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-4: ordinal not in range(128)

#!/usr/bin/python# -*- coding: utf-8 -*- 解决方法: 可以看到我的版本是2.6的,所以打开/usr/lib64/python2.6/site.py 红框里本来是0,把这个0改为1就可以了. Python UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-4: ordinal not in range(128)

【Python】UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-3

问题如下: UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-3 解决方法: 程序开始加上下面两句 1 reload(sys) 2 sys.setdefaultencoding( "utf-8" ) 另外:当字符串里有 \n.\t.\r时,json.loads()失效,异常,要去掉 [Python]UnicodeEncodeError: 'ascii' codec can't encode c

python输出字符串,UnicodeEncodeError: 'ascii' codec can't encode characters in position问题

http://blog.sina.com.cn/s/blog_64a3795a01018vyp.html 参考于这个博主,我自己做一个笔记. 把一个列表转换成字符串输出的时候出现了UnicodeEncodeError: 'ascii' codec can't encode characters in position 32-34: ordinal not in range(128)问题,使用的是ulipad编译器. 解决方法1: 在开头加上 import sys reload(sys) sys.

Python编码问题:UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-1: ordinal not in range(12

今天安装了PyScripter编辑器,刚要写代码,突然就出现异常: <span style="font-size:14px;color:#ff0000;">>>> Traceback (most recent call last): File "<string>", line 378, in findModuleOrPackage File "<string>", line 367, in f

python 中UnicodeEncodeError: &#39;ascii&#39; codec can&#39;t encode characters in position 0-7: ordinal not in range(128)的解决方法

SyntaxError: Non-ASCII character '\xe6' in file出现这个问题是因为中文出现在了代码中,解决方法为在代码最上面添加:#coding:utf-8 UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-7: ordinal not in range(128) 这个问题则是在程序中写入中文时出错,解决方法为在开头添加: import sys reload(sys) sy