【发布时间】: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