0
我有一個JSON,我必須操作並且代碼如下所示。我不得不操縱數據,因爲上傳到BigQuery時無法使用數據的無效部分。如何使用Python向JSON添加換行符功能
import json
with open('firetobq_peripheral.json', 'r') as in_file:
dicts = []
for line in in_file.readlines() :
d = json.loads(line.strip())
if d.get('Peripherals'):
del d['Peripherals']
dicts += [d]
with open('firetobq_peripheral5.json', 'w') as out_file:
out_file.write(json.dumps(dicts))
out_file.write("\n")
但是out_file.write("\n")
沒有使JSON寫每一組數據對新行像它在前面JSONs我曾上。有誰知道爲什麼會發生這種情況,以及如何解決這個問題?謝謝您的幫助。
我需要的數據來呈現這樣
{"AppName": "DataWorks", "foundedPeripheralCount": 1, "version": "1.6.1(8056)", "deviceType": "iPhone 6", "createdAt": "2017-04-05T07:05:30.408Z", "updatedAt": "2017-04-05T07:08:49.569Z", "connectedPeripheralCount": 1, "iOSVersion": "10.2.1"}
{"objectId": "20H5Hg2INB", "foundedPeripheralCount": 0, "DeviceVendorID": "5B7F085E-B3B6-4270-97DC-F42903CDEAC1", "version": "1.3.5(5801)", "deviceType": "iPhone 6", "createdAt": "2015-11-10T06:16:45.459Z", "updatedAt": "2015-11-10T06:16:45.459Z", "connectedPeripheralCount": 0, "iOSVersion": "9.1"}
{"AppName": "DataWorks", "foundedPeripheralCount": 2, "version": "1.6.2(8069)", "deviceType": "iPhone 6s", "createdAt": "2017-04-12T10:05:05.937Z", "updatedAt": "2017-07-06T07:33:02.006Z", "connectedPeripheralCount": 8, "iOSVersion": "10.2.1"}
我認爲你應該這樣做'out_file.write(「\\ n」)' – Sankar
你能否提供一個[mcve]的一些數據? –
我已經添加了一個例子 –