从文件中读取字典并打印键
Read dictionary from a file and print the keys
基本上我试图在 python 中编写一个代码,从文件中获取字典。它应该打印显示的键。
{
"Name": "namename",
"Surname": "klsajdak",
"Mhtrwo": "lsdkaslkd",
"Phone": ["545454545454", "4554545454545"],
"Age": 84,
"kids": {
"Name": "Zero",
"Age": 0
}
}
my_dict = open("9listes.txt", "r")
for key,value in my_dict.items():
print("Key : {}".format(key))
您可以通过使用 json.load()
实现此目的:
import json
with open('9listes.txt') as f:
my_dict = json.load(f) # `my_dict ` is the `dict` you need
# To print "key" & "value", uncomment below lines:
# for key, value in my_dict.items():
# print("Key: {}, Value: {}".format(key, value))
参考json.load()
document了解更多详情。
基本上我试图在 python 中编写一个代码,从文件中获取字典。它应该打印显示的键。
{
"Name": "namename",
"Surname": "klsajdak",
"Mhtrwo": "lsdkaslkd",
"Phone": ["545454545454", "4554545454545"],
"Age": 84,
"kids": {
"Name": "Zero",
"Age": 0
}
}
my_dict = open("9listes.txt", "r")
for key,value in my_dict.items():
print("Key : {}".format(key))
您可以通过使用 json.load()
实现此目的:
import json
with open('9listes.txt') as f:
my_dict = json.load(f) # `my_dict ` is the `dict` you need
# To print "key" & "value", uncomment below lines:
# for key, value in my_dict.items():
# print("Key: {}, Value: {}".format(key, value))
参考json.load()
document了解更多详情。