轻松解决 UnicodeEncodeError: ‘ascii‘ codec can‘t encode characters in position 0-4: ordinal not in range

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

解决方法:

一、大众方法

网上很多方法在python文件头添加几行代码如下所示:

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

二、简单方法
直接使用str()函数,例如:

print "文件名:",str(am.name)

三、字符编码解码
编码根据实际情况进行修改:

str1.decode('gb2312').encode('utf8')

更多有趣的实用技术,请查看对应的专栏。

官网首页:http://www.52014991.xyz/home.html

福利实惠:http://www.52014991.xyz/contact.html

功能jar包:http://www.52014991.xyz/products.html

博客主页:https://me.csdn.net/u014374009

代码主页:https://github.com/YouAreOnlyOne?tab=repositories

团队成员:http://www.52014991.xyz/team.html

更多干货资源请关注作者的专栏。

留言点赞关注,我们一起分享AI学习与发展的干货。

你可能感兴趣的:(Python相关)