【发布时间】:2017-11-07 03:17:24
【问题描述】:
我对 Python 还是很陌生,我想知道如何将文本文件中已经采用 key:value 形式的内容转换为 Python 字典?
例如。
2:red
3:orange
5:yellow
6:green
(每个键:单独一行的值)
我查看了其他帖子,但它们似乎都不起作用,我知道我做错了什么。到目前为止,我有:
def create_colours_dictionary(filename):
colours_dict = {}
file = open(filename,'r')
contents = file.read()
for key in contents:
#???
return colours_dict
【问题讨论】:
标签: python file dictionary