免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 17768 | 回复: 5
打印 上一主题 下一主题

AttributeError: 'module' object has no attribute 'setdefaultencoding' [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-07-24 23:04 |只看该作者 |倒序浏览
windows 环境下报错:
  1. import sys
  2. reload(sys)
  3. sys.setdefaultencoding( 'utf8')

  4. #--*- coding:gbk --*--
  5. import pickle as p
  6. #import pickle as p

  7. shoplistfile = 'shoplist.data'
  8. # the name of the file where we will store the object

  9. shoplist = ['apple', 'mango', 'carrot']

  10. # Write to the file
  11. f = open(shoplistfile, 'wb')
  12. p.dump(shoplist, f) # dump the object to a file
  13. f.close()

  14. del shoplist # remove the shoplist

  15. # Read back from the storage
  16. f = open(shoplistfile)
  17. storedlist = p.load(f)
  18. print ('the following text comes from data e:\lib\shoplist.data')
  19. print (storedlist)
复制代码
  1. Traceback (most recent call last):
  2.   File "E:/lib/pickling.py", line 3, in <module>
  3.     sys.setdefaultencoding( 'utf8')
  4. AttributeError: 'module' object has no attribute 'setdefaultencoding'
复制代码

论坛徽章:
0
2 [报告]
发表于 2011-07-24 23:29 |只看该作者
看网上的解决方法:
reload(sys)
sys.setdefaultencoding('utf-8')

但是 依然报错:
  1. Traceback (most recent call last):
  2.   File "E:/lib/pickling.py", line 2, in <module>
  3.     reload(sys)
  4. NameError: name 'reload' is not defined
复制代码

论坛徽章:
0
3 [报告]
发表于 2011-07-24 23:35 |只看该作者
setdefaultencoding( name)

Set the current default string encoding used by the Unicode implementation. If name does not match any available encoding, LookupError is raised. This function is only intended to be used by the site module implementation and, where needed, by sitecustomize. Once used by the site module, it is removed from the sys module's namespace. New in version 2.0.

论坛徽章:
0
4 [报告]
发表于 2011-07-24 23:59 |只看该作者
回复 3# mymoflx

我的版本是3.1 的,windows 环境 ,网上有很多介绍这个的 site.py的,按照大家给的办法,加上reload(sys)还是报错。。。

论坛徽章:
0
5 [报告]
发表于 2011-07-25 13:55 |只看该作者
看看你当前目录下是否有这么一个叫sys.py的文件

论坛徽章:
0
6 [报告]
发表于 2011-07-25 14:09 |只看该作者
如果没猜错的话 你用的是python3 版本

reload从builtsin模块移除了 可以使用imp.reload()
python3默认支持unicode编码,所以'setdefaultencoding'也从sys模块移除
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP