简体   繁体   中英

how to load json file output(subprocess) into a variable in python?

have a python code that generates random data everytime . i run it using subprocess.

first.py

code :

for _ in range(50):
sms =  {

    "name": fake.name(),
    "email": fake.email() 
      }


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

subprocess:

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

output :

    {

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

how to store every output generated by values in a dictionaries or any other useful format in 1,2,3,4,...50 . so that i can use them later.

example:

 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 will contain the output generated by the command

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

Let me know if you don't figure it out

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM