代码:
userid1='u123'
userid2='u124'
ids= (userid1,userid2)
fake = Faker('en_US')
for ind in ids:
for idx in range(1):
sms = {
"id": ind ,
"name": fake.name(),
"email": fake.email(),
"gender": "MALE",
}
f_name = '{}.json'.format(ind)
with open(f_name, 'w') as fp:
#Save the dictionary
json.dump(sms, fp, indent=4)
print(sms)
file1 = filename.json ( how to get the *ind* value here i.e., userid)
fd1=open("filename.json")
json_content1 = fd1.read()
fd1.close()
如何在f_name = '{}.json'.format(ind)
处打开已保存的文件。无需手动提及文件名。文件名使用 ind 保存。那么如何在此处使用ind并打开文件
答案 0 :(得分:1)
此代码可以帮助您从json文件中获取数据:您可以通过键入data [“ file-of-filed”]来从json数据中获取任何文件:
import json
userid1='json_file1'
ids= [userid1]
for ind in ids:
f_name = '{}.json'.format(ind)
with open(f_name, 'r') as outfile:
data = json.loads(outfile.read())
print(data["name"])
print(data)
这是一个例子:
file.json :
{
"name": "Ghassen",
"apiVersion": "v1"
}
输出:
Ghassen
{'name': 'Ghassen', 'apiVersion': 'v1'}