【发布时间】:2016-09-11 04:49:11
【问题描述】:
以下代码不起作用:
with open("testfile.txt",'w') as fw:
for key in a:
fw.write(a[key])
以下是错误:
Traceback (most recent call last):
File "<stdin>", line 3, in <module>
TypeError: expected a character buffer object`
以下代码有效:
with open('myfile.txt', 'w') as f:
for key, value in a.items():
f.write('%s:%s\n' % (key, value))
有人可以帮我理解第一个区块出了什么问题吗?
【问题讨论】:
-
你能发布
a字典值吗 -
a = {1:2,2:3,3:4}
标签: python python-2.7 file dictionary