You can’t just concatenate two JSON strings to make valid
JSON (or combine them by tacking
',n'to the end of
each). Instead, you could combine the two (as Python objects) into a Python
list, then use
json.dumpto write
it to a file as JSON:
import jsonimport globresult = []for f in glob.glob("*.json"): with open(f, "rb") as infile: result.append(json.load(infile))with open("merged_file.json", "wb") as outfile: json.dump(result, outfile)
If you wanted to do it without the (unnecesssary) intermediate step of parsing
each JSON file, you could merge them into a list like this:
import globread_files = glob.glob("*.json")with open("merged_file.json", "wb") as outfile: outfile.write('[{}]'.format( ','.join([open(f, "rb").read() for f in read_files])))
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)