如何保存循环中的子流程的输出(json文件) [英] how to save the output (json file)of a subprocess that is in a loop

查看:77
本文介绍了如何保存循环中的子流程的输出(json文件)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此代码发送会生成一个随机的json文件,其中包含提供的用户ID和顺便说一句 范围也给定了.

this code send generates a random json file of user ids provided and btw range is also given..

因此此代码为每个用户输出50个json.

so this code outputs 50 jsons for each user.

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"])


此loop.py代码使第一个代码循环20次

this loop.py code loops the first code 20 times

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

我需要为每个循环保存loop.py代码的输出.并存储该json.示例:我们在loop.py中将第一个代码循环20次,因此存储的输出应类似于5cda1.json ........ 5cda20.json和6cda1.json ..... 6cda20.json

i need to save the output of loop.py code for each loop. and store that json. example : we are looping the first code for 20 times in loop.py so ineed the output stored should be like 5cda1.json ........ 5cda20.json and 6cda1.json..... 6cda20.json

这里我们给了两个用户ID ids= ('5cda','6cda'),所以输出总共是40个json文件.

here we are giving two user ids ids= ('5cda','6cda') so output would be total 40 json files.

推荐答案

您希望将文件保存逻辑带入for ind in ids:循环内,并在整个循环中使用索引来保存具有不同名称的文件. /p>

You would want to bring the file saving logic inside the for ind in ids: loops, and use the index in the overall loop to save the file with different names.

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 i 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)

        #Append the index to the file here, and save the file
        f_name = '{}{}.json'.format(ind,i+1)
        with open(f_name, 'w') as fp:
            # Save the dictionary
            json.dump(dct, fp, indent=4)
            print('saved {}'.format(f_name))

根据您的要求,您将获得40个文件,并且所有文件中的内容都是唯一的.

You will get 40 files as per your requirements, with unique content in all files.

这篇关于如何保存循环中的子流程的输出(json文件)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆