我有一段代码,其中在文本文件中输出字典,我有一个问题是否可以使用搁置模块来完成?
我有这段代码
dict3 = {'12345': ['paper', '3'], '67890': ['pen', '78'], '11223': ['olive', '100'], '33344': ['book',
'18']}
output = open("output.txt", "a", encoding='utf-8')
for k, v in dict3.items():
output.writelines(f'{k} {v[0]} {v[1]}\n')
output.close()
执行此代码时,我得到以下结果:
12345 paper 3
67890 pen 78
11223 olive 100
33344 book 18
那么,也许有人知道如何做同样的事情,但使用搁置模块?
I have this piece of code
dict3 = {'12345': ['paper', '3'], '67890': ['pen', '78'], '11223': ['olive', '100'], '33344': ['book',
'18']}
output = open("output.txt", "a", encoding='utf-8')
for k, v in dict3.items():
output.writelines(f'{k} {v[0]} {v[1]}\n')
output.close()
When this code is executed I have this result:
12345 paper 3
67890 pen 78
11223 olive 100
33344 book 18
So, maybe someone knows how to do the same, but using the shelve module?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于
shelve
书架闻起来像字典,因此您只需使用.update()
将该字典写入书架,然后使用.items()
即可读取:输出:
Since
shelve
shelves smell like dictionaries, you can just use.update()
to write that dict into a shelf, then.items()
to read:Output: