UnicodeEncodeError: 'latin-1' codec can't encode characters in position 0-3: ordinal not in range(256)

今天使用MySQLdb往MySQL插入中文数据遇到一个异常:

UnicodeEncodeError: ‘latin-1‘ codec can‘t encode characters in position 0-3: ordinal not in range(256)

解决办法:

conn = MySQLdb.connect(host="localhost", user="root", passwd="root", db="chemical", charset="utf8") 

UnicodeEncodeError: 'latin-1' codec can't encode characters in position 0-3: ordinal not in range(256)

时间: 2024-12-28 01:30:25

UnicodeEncodeError: 'latin-1' codec can't encode characters in position 0-3: ordinal not in range(256)的相关文章

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

UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-13: ordinal not i│ n range(128)

python保持网页文件遇到的错误,归根结底还是编码问题,改一下要保存的数据为utf-8就好了. 如下最简单: import sys reload(sys) sys.setdefaultencoding('utf-8') 更详细一些的python编码可以见下面文章: http://python.jobbole.com/85482/ 其实更好的解决方式是使用Python3,哼 UnicodeEncodeError: 'ascii' codec can't encode characters in

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\

UnicodeEncodeError: 'ascii' codec can't encode characters in position 820-823: ordinal not in range(128)

真是奇怪了,在itermi里 print(data) 就能直接运行,而在sublime里,就非得写成这样 print(data.encode('utf-8')) UnicodeEncodeError: 'ascii' codec can't encode characters in position 820-823: ordinal not in range(128)

pip install 安装出现问题:UnicodeEncodeError: 'ascii' codec can't encode characters in position XX的解决办法

pip install 安装出现问题:UnicodeEncodeError: 'ascii' codec can't encode characters in position XX的解决办法 转自csdn 我在cmd中运行: pip install wheel时出现如下的问题 :  解决办法如下: **在Python目录 Python27\Lib\site-packages 建一个文件sitecustomize.py 里面的内容是:** import sys sys.setdefaultenc

rfw使用接口库测试--FAIL : UnicodeEncodeError: 'ascii' codec can't encode characters in position

使用requests库进行接口测试时 需要构造dictionary来填充http的文件头headers 填充后请求报错 FAIL : UnicodeEncodeError: 'ascii' codec can't encode characters in position 解决方法 在文件RequestsKeywords.py中加入 import sys reload(sys) sys.setdefaultencoding( "utf-8" ) 解决 rfw使用接口库测试--FAIL

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

由于数据库中存有中文字符,在用python select的时候报错: UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-78: ordinal not in range(128) Solution: 在python的开头处添加下边的三行即可解决问题: import sys reload(sys)sys.setdefaultencoding('utf8') UnicodeEncodeError: 'asc

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-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)