我试图从目录中读取多个json文件,然后通过循环将它们添加到其他json文件中,但是我无法在单个for循环中读取它们。
我尝试将循环添加到json“数据”中,我尝试将循环边,内边甚至同一行(我无法弄清楚)
import csv
import os
from itertools import groupby
import json
def joinfinal(infile1, file):
path = 'C:/Users/upadh/PycharmProjects/Practice/Aditya/File'
fileList = os.listdir(path)
arr = []
with open('Files/Out1.csv', 'r') as infile1:
r = csv.DictReader(infile1)
data = [dict(d) for d in r]
arr = [file]
n = (len(arr))
for i in fileList:
file = open(os.path.join('File/' + i), 'r')
groups = []
for k, g, in (groupby(data, lambda r: (r['Site Name'], r['Label'],
r['Units'], r['device']))):
groups.append({
"Type": "UPDATE",
"resource": {
"site ": k[0],
"name": k[1],
"unit": k[2],
"device": k[3],
"data":open(os.path.join('File/' + i), 'r').read()if i,
"type": "QUANTITY",
"status": "ACTIVE"
}
})
with open("Result.json", "w")as w:
w.write(json.dumps(groups[:20], indent=4))
我想要在json的for循环中增加,应在数据中添加其他文件。