【问题标题】:How to Save the Output of a script that loops a python script using subprocess?如何保存使用子进程循环python脚本的脚本的输出?
【发布时间】:2019-05-15 05:09:25
【问题描述】:

如何保存循环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'])

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

curl.py 代码生成随机 json 并将输出保存到 json 文件

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


【问题讨论】:

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


    【解决方案1】:

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

    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",
    .....
    

    【讨论】:

    猜你喜欢
    • 2021-08-30
    • 2015-07-16
    • 2016-07-28
    • 1970-01-01
    • 2015-01-21
    • 2017-11-19
    • 2014-12-17
    • 1970-01-01
    • 2015-09-22
    相关资源
    最近更新 更多