如果我有这样的文件:
1000:80
200:90
400:60
我如何将这些值放在dict中?像这样:
d = {'1000':80, '200:90', '400':60}
答案 0 :(得分:0)
像这样(使用generator expressions和dictionary comprehension):
with open("myfile.txt") as f:
my_dict = {x: int(v) for x, v in (line.split(":") for line in f)}
print(my_dict)
{'200':90,'400':60,'1000':80}