如何保存已在另一个Python文件被环Python文件的输出?(how to save the ou

2019-10-29 12:21发布

此代码发送生成提供的用户ID的随机JSON文件和顺便说一句范围也给出..

因此,此代码输出50个jsons为每个用户。

让我们命名这个代码first.py

此代码随机地生成的JSON和通过使用grpcul我会发送数据。

所以first.py代码:

import faker
import json
from faker import Faker
import random
from random import randint
import subprocess
import json
import os
#subprocess.call([""])
from pprint import pprint

ids= ('5cda','6cda')

fake = Faker('en_US')

for ind in ids:
    cont = []
    #Overall dictionary with first and user_ids
    dct = {}
    for idx in range(50):

        sms =  {
            "id":"AB-asfgw",
            "body": fake.text(),
            "mime": fake.ean(),
            "hashed": fake.ean(),
            "pid": fake.ean(),
            "user_id": ind,
            "text": fake.sentence()
        }
        cont.append(sms)

    dct['messages'] = cont
    dct['user_id'] = ind
    #print(dct)
    f_name = '{}.json'.format(ind)
    with open(f_name, 'w') as fp:
        #Save the dictionary
        json.dump(dct, fp, indent=4)
        print('saved {}'.format(f_name))    

auth = "authorization: token 1324"
file = "5cda.json"
fd=open("5cda.json")
json_content = fd.read()
fd.close()


subprocess.run(["grpcurl", "-plaintext","-H", auth,"-d",json_content,"-format","json","100.20.20.1:5000","api.Service/Method"])


使用子进程来运行该代码和subprocess.run(["grpcurl", "-plaintext","-H", auth,"-d",json_content,"-format","json","100.20.20.1:5000","api.Service/Method"])

所以我在另一个程序试图循环程序......所以我需要保存部分loop.py做,这样我可以限制的第一个程序

所以我写了另一个小代码回路的第一个代码。 但我面临的问题是存储数据的即时发送,因为我使用摊贩库,它生成随机数据。

所以,我的第二个代码让调用它作为second.py

代码:

这second.py代码循环的第一码20倍

from datetime import datetime
import faker
import json
from faker import Faker
import random
from random import randint
import subprocess
import json
import os
#subprocess.call([""])
from pprint import pprint
import subprocess
import sys



for i in range(20):
    subprocess.call(['python','grploop1.py'])

这上面的代码将运行并发送该数据和数据歌厅sussecfully调用。 什么是需要的是存储数据发送即时消息,因为它是从摊贩被随机发送。

所以OUTPUT我需要的是在我的second.py代码我需要以某种方式储存JSON文件我正在循环和发送。 我需要存储的数据。 阅读subprocess.output确实如此,但无法弄清楚。我需要实施应在second.py和first.py并没有改变做....

文章来源: how to save the output of a python file that has been looped in another python file?