如何将json文件输出(子进程)加载到python中的变量中?

时间:2019-06-19 18:06:08

标签: python pandas loops subprocess

有一个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

1 个答案:

答案 0 :(得分:0)

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']