3

I have a python code that outputs json

import json 
from faker import Faker
import random
from random import randint
import subprocess
fake = Faker('en_US')

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

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

print(sms)

subprocess:

x=subprocess.Popen([" python"," first.py"],shell=True, stdout=subprocess.PIPE)
output = x.communicate()
print(output)

output I am getting :

(b'{\n  "name": "elmoroy",\n  "email":"ssbyt@gmail.com"}\n', None)

output I need :

{
"name": "elmoroy",
"email":"ssbyt@gmail.com
}

If I call output["name"] it should return elmoroy.

Arkistarvh Kltzuonstev
  • 6,824
  • 7
  • 26
  • 56
  • The `first.py` isn't outputting anything to `stdout`, so `x` will be nothing. The script creates a file `abc.json`, so you can open a read that file when the subprocess is finished. Alternatively, you could `print(json.dumps(sms))` and write some output to `stdout`. – martineau Jun 20 '19 at 06:46
  • updated pl check –  Jun 20 '19 at 07:12
  • `print(sms)` would _not_ produce the output you say you're getting. – martineau Jun 20 '19 at 07:35
  • `x=subprocess.Popen([" python"," first.py"],shell=True, stdout=subprocess.PIPE) output = x.communicate() print(output)` if you run this youll get it –  Jun 20 '19 at 07:39

2 Answers2

3

communicate() returns a tuple (stdout_data, stderr_data), the output you need is in output[0] which is a string representation of the dictionary you need, you can then use my_dict = json.loads(output[0]) to get a dictionary.

UPDATE : to run this in a loop

my_dict = {}
for i in range(20):
    x=subprocess.Popen([" python"," first.py"],shell=True, stdout=subprocess.PIPE)
    output = x.communicate()
    my_dict.update({i: json.loads(output[0])})

my_dict would hold 20 dictionaries of the printed sms variable

Rotem Tal
  • 739
  • 5
  • 11
  • how to store if i loop the code for 20 times ? if i loop it for 20 times and i need to store all the output generated. –  Jun 20 '19 at 07:37
  • According to your code, looping for 20 times would still print 1 sms variable with 1 dictionary in it, or have I not understood your question? – Rotem Tal Jun 20 '19 at 07:41
  • i need to loop this `x=subprocess.Popen([" python"," first.py"],shell=True, stdout=subprocess.PIPE) output = x.communicate() print(output)` for 20 times and store all the outputs in dicts –  Jun 20 '19 at 07:44
  • 1
    `my_iter_dict = {i: json.loads(subprocess.Popen([" python"," first.py"],shell=True, stdout=subprocess.PIPE).communicate()[0]) for i in range(20)}` alternatively, inside a for loop (with an existing dict var name `my_dict` you could do `my_dict.update({i: json.loads(output[0])})` I'm using the iteration number as keys, you could use whatever u'd like – Rotem Tal Jun 20 '19 at 07:49
  • changed `my_dict ` to `my_iter_dict` getting `i` is not defined –  Jun 20 '19 at 09:28
  • 1
    `i` is the loop's index, i'll edit my answer so it would make more sense – Rotem Tal Jun 20 '19 at 09:31
0

Maybe you should try using json.load, like this:

with open('abc.json') as in_file:
    obj = json.load(in_file)
print(obj)

Refer to 'Decoding JSON' in json — JSON encoder and decoder:

---edit---

Try this:

First, you get a file like:

import json

for _ in range(1):
    sms =  {
            "name": 'some name',
            "email": 'some email',
            "location": "usa"
    }


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

Then, you get another file like:

import json

with open('abc.json') as in_file:
    sms = json.load(in_file)
print(sms)

Execute first file and then the second, and you can see the second file parse the file content into a json object.

fishoverflow
  • 91
  • 1
  • 5
  • i wont work, u didnt mentioned sms in load as i have the data that should be printed –  Jun 20 '19 at 06:12
  • I provide more detail in my answer. Maybe you can have another try. – fishoverflow Jun 20 '19 at 06:26
  • i dont want to save the output into a another file, i need to load from print function and use it for later –  Jun 20 '19 at 06:41
  • If you want to load json from string, you can try `json.loads`. For example, `json.loads('{"key": "value"}')` will get you an json object. – fishoverflow Jun 20 '19 at 06:54
  • i need to capture data from print(output) and store them in a dict –  Jun 20 '19 at 07:49