-
Python基础02
普通类 -
- 支持
- 批判
- 提问
- 解释
- 补充
- 删除
-
-
TypeError: 'dict_keys' object does not support indexing
[python] view plaincopy
- import random
- outcomes = {'heads':0, 'tails':0}
- sides = outcomes.keys()
- print(sides[0])
会产生错误:TypeError: 'dict_keys' object does not support indexing
这是由于python3改变了dict.keys,返回的是dict_keys对象,支持iterable 但不支持indexable,我们可以将其明确的转化成list:
[python] view plaincopy
- import random
- outcomes = {'heads':0, 'tails':0}
- sides = list(outcomes.keys())
- print(sides[0]) # 输出tails python3.0
-
pickle.load 'str' does not support the buffer interface
#使用pickle模块从文件中重构python对象
import pprint, pickle
#pkl_file = open('data.pkl')#之前没有添加编码格式 python2.0
pkl_file = open('data.pkl', 'rb')#python3.0
data1 = pickle.load(pkl_file)
pprint.pprint(data1)
data2 = pickle.load(pkl_file)
pprint.pprint(data2)pkl_file.close()
来源:http://www.cnblogs.com/pzxbc/archive/2012/03/18/2404715.html
-
-
- 标签:
- python
- str'
- 39
- sides
- buffer
- data.pkl'
- support
- pickle.load
- open
- object
-
加入的知识群:
学习元评论 (0条)
聪明如你,不妨在这 发表你的看法与心得 ~