如何在 Python 中将 JSON 文件输出(子进程)加载到变量中



有一个每次生成随机数据的python代码。 我使用子进程运行它。

first.py

法典:

for _ in range(50):
sms =  {
    "name": fake.name(),
    "email": fake.email() 
      }

with open('sfirst.json', 'w') as outfile:
    json.dump(sms, outfile)

子流程:

  subprocess.call(["python","first.py")

输出:

    {
    "name": "elmaro",
    "email": "elmaro@gmail.com" 
      }

如何以字典或任何其他有用的格式以 1,2,3,4 存储值生成的每个输出,...50 .以便我以后可以使用它们。

例:

 here we are looping 50 times so 
 {
 "name1": elmaro,
  "email1": elamro@gmail.com,
 "name2": spetus,
  "email2": spetus@gmail.com
    ........
  ........
   }
  upto 50 times should be stored and when i call 
  data[email45] it should return the value stored
from subprocess import check_output
out = check_output(["python","first.py"])

out 将包含命令生成的输出

output_dict = dict()
output_dict['you need code to compute this'] = out['name']
output_dict['you need code to compute this as well'] = out['email']

如果你不明白,请告诉我

output_dict = dict()
for loop in range(50):
    out = check_output(["python","first.py"])
    emailKey = str(loop) + 'email'
    nameKey = str(loop) + 'name'
    output_dict[nameKey] = out['name']
    output_dict[emailKey] = out['email']

最新更新