简体   繁体   中英

Appending data to an existing Json file

i have json file of the following format:

{"CustomerNo": "858017D", "SalesOrder": "ERI2380", "Brand": "HONAC", "Item": "HL10CESWB", "Qty": "1", "Month": "6","time": "2018-06-19T16:28:58Z"}
{"CustomerNo": "072881D", "SalesOrder": "W619091", "Brand": "HONAC", "Item": "MO10CESWK", "Qty": "-1", "Month": "6","time": "2018-06-08T12:36:29Z"}
{"CustomerNo": "072881D", "SalesOrder": "W642501", "Brand": "HONAC", "Item": "MO08CESWK", "Qty": "1", "Month": "6",  "time": "2018-06-19T08:20:51Z"}
{"CustomerNo": "072866D", "SalesOrder": "W645370", "Brand": "HONAC", "Item": "MN10CESBB", "Qty": "1", "Month": "6",  "time": "2018-06-19T16:36:22Z"}

Need to append to an existing file which has data in a similar format. But the resulting file is getting all messed up. I need append this file several times a day. The code looks like:

csvfile1 = open('daily.csv', 'r')
jsonfile1 = open('test3.json', 'a')
fieldnames = list(raw_data.columns.values)
reader1 = csv.DictReader( csvfile1, fieldnames)


next(reader1)
jsonfile1.write('\n')
pos = next1 = 0
for row in reader1:
    pos = next1
    next1 += len(row) # compute position of beginning of next line
    json.dump(row, jsonfile1)
    jsonfile1.write('\n')
jsonfile1.truncate(pos)

The final output is not exactly the append. It is some malformed file with incomplete json objects.

Consider closing files when you're finished (or use with-as). Also not sure why you calculate position to truncate (on 'a' mode) at the end of appending.

import csv
import json

fieldnames = ("CustomerNo", "SalesOrder", "Brand", "Item", "Qty", "Month", "time")

# Handle the case when there is no newline at the end. Format a file before appending.

with open('daily.csv', 'r') as csvfile1:
    with open('test3.json', 'a') as jsonfile1:
        for row in csv.DictReader(csvfile1, fieldnames):
            json.dump(row, jsonfile1)
            jsonfile1.write('\n')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM