【发布时间】:2013-12-11 19:49:25
【问题描述】:
所以我正在编写一个函数来打开一个文件,读取它并将其内容放入字典中。
基本上我正在阅读的文件如下所示:
Bread 10
Butter 6
Cheese 9
Candy 11
Soda 5
我想确保我的字典有这种形式:
{ 'bread': 10, 'butter': 6, 'cheese': 9, 'candy': 11, 'soda': 5 }
那么,我怎样才能确保这些字词保持字符串,并且我将输出数字为int?
到目前为止,这就是我打开文件的方式,但不知道如何继续。
def preberi_inventar(dn0501):
f = open("dn0501.txt", "r")
line = f.readlines()
f.close()
【问题讨论】:
-
第一步是开始使用
line...也许你可以split它? -
我已经得到答案了,还是谢谢你:)
-
ups,没看到 :)
标签: python string file-io directory