如何保存使用子进程循环python脚本的脚本输出?

时间:2019-05-15 05:09:26

标签: python python-3.x loops for-loop subprocess

如何保存循环python脚本的python脚本的输出

from datetime import datetime
import faker
import json
from faker import Faker
import random
from random import randint
import subprocess
import json
import os
from pprint import pprint
import subprocess
import sys


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

此代码循环我的python脚本,该脚本生成随机的json文件。我每次循环时都需要保存出口。

可生成随机json并将输出保存到json文件的curl.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','7f36')

fake = Faker('en_US')

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


        sms =  {
            "id":"AB-Iasd",
            "body": fake.sentence(),
            "reae": fake.ean(),
            "ashe": fake.ean(),
            "id2": fake.ean(),
            "user_id": ind,
            "pid": fake.sentence()
        }
        cont.append(sms)
    #Use a dictionary to save cont list to first key, and ind to user_ids key
    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))


1 个答案:

答案 0 :(得分:0)

您无需添加外部文件curlloop1.py到另一个文件中,只需添加一个整体的for循环即可运行20次,将所有字典收集在一个列表中,然后将该列表另存为字典在一个文件中

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','7f36')

fake = Faker('en_US')

msg_list = []

#Overall loop to run inner loop 20 times
for _ in range(20):
    #Loop to generate messages
    for ind in ids:
        cont = []
        #Overall dictionary with first and user_ids
        dct = {}
        for idx in range(20):


            sms =  {
                "id":"AB-Iasd",
                "body": fake.sentence(),
                "reae": fake.ean(),
                "ashe": fake.ean(),
                "id2": fake.ean(),
                "user_id": ind,
                "pid": fake.sentence()
            }
            cont.append(sms)
        #Use a dictionary to save cont list to first key, and ind to user_ids key
        dct['messages'] = cont
        dct['user_id'] = ind
        msg_list.append(dct)


#Saving overall data to a single dictionary and to a file
data = {"data":msg_list}

f_name = 'data.json'
with open(f_name, 'w') as fp:
    #Save the dictionary
    json.dump(data, fp, indent=4)
    print('saved {}'.format(f_name))

data.json看起来像

{
    "data": [
        {
            "messages": [
                {
                    "id": "AB-Iasd",
                    "body": "Up west accept third success bad.",
                    "reae": "0181541986524",
                    "ashe": "1490107025812",
                    "id2": "4087146706078",
                    "user_id": "5cda",
                    "pid": "Color wish method population affect."
                },
                {
                    "id": "AB-Iasd",
                    "body": "Human expect news son room recognize beautiful.",
                    "reae": "9417635708941",
                    "ashe": "0920108608482",
                    "id2": "8218562976929",
.....