我正在从firebase下载数据,将其导出到json中。在此之后我尝试将其上传到bigquery但我需要删除新的换行符以进行大查询以接受它。
{ "ConnectionTime": 730669.644775033,
"objectId": "eHFvTUNqTR",
"CustomName": "Relay Controller",
"FirmwareRevision": "FW V1.96",
"DeviceID": "F1E4746E-DCEC-495B-AC75-1DFD66527561",
"PeripheralType": 9,
"updatedAt": "2016-12-13T15:50:41.626Z",
"Model": "DF Bluno",
"HardwareRevision": "HW V1.7",
"Serial": "0123456789",
"createdAt": "2016-12-13T15:50:41.626Z",
"Manufacturer": "DFRobot"}
{
"ConnectionTime": 702937.7616419792,
"objectId": "uYuT3zgyez",
"CustomName": "Relay Controller",
"FirmwareRevision": "FW V1.96",
"DeviceID": "F1E4746E-DCEC-495B-AC75-1DFD66527561",
"PeripheralType": 9,
"updatedAt": "2016-12-13T08:08:29.829Z",
"Model": "DF Bluno",
"HardwareRevision": "HW V1.7",
"Serial": "0123456789",
"createdAt": "2016-12-13T08:08:29.829Z",
"Manufacturer": "DFRobot"}
这就是我需要的方法,但除了手动操作之外无法弄清楚如何做到这一点。
{"ConnectionTime": 730669.644775033,"objectId": "eHFvTUNqTR","CustomName": "Relay Controller","FirmwareRevision": "FW V1.96","DeviceID": "F1E4746E-DCEC-495B-AC75-1DFD66527561","PeripheralType": 9,"updatedAt": "2016-12-13T15:50:41.626Z","Model": "DF Bluno","HardwareRevision": "HW V1.7","Serial": "0123456789","createdAt": "2016-12-13T15:50:41.626Z","Manufacturer": "DFRobot"}
{"ConnectionTime": 702937.7616419792, "objectId": "uYuT3zgyez", "CustomName": "Relay Controller", "FirmwareRevision": "FW V1.96", "DeviceID": "F1E4746E-DCEC-495B-AC75-1DFD66527561", "PeripheralType": 9, "updatedAt": "2016-12-13T08:08:29.829Z", "Model": "DF Bluno", "HardwareRevision": "HW V1.7", "Serial": "0123456789", "createdAt": "2016-12-13T08:08:29.829Z", "Manufacturer": "DFRobot"}
我正在使用python加载json,读取它然后写一个新的但无法弄清楚正确的代码。谢谢!
这是我的python代码的大纲
import json
with open('nospacetest.json', 'r') as f:
data_json=json.load(f)
#b= the file after code for no line breaks is added
with open('testnoline.json', 'w') as outfile:
json.dump=(b, outfile)
答案 0 :(得分:3)
您需要确保indent=None
{j}时数据with open('testnoline.json', 'w') as outfile:
json.dump(data_json, outfile, indent=None)
:
indent
从文档引用:
如果
None
是非负整数,则JSON数组元素和对象成员将使用该缩进级别进行漂亮打印。缩进级别为0或负数只会插入换行符。$('.category-select, .category-number').change(function() { var rowParent = $(this).closest(".row").parent(); var obj = []; $(rowParent).children(".row").each(function() { var row = $(this) var selectVal = $(row).find("select").val(); var inputVal = $(row).find("input").val(); if (inputVal !== "") { for (i = 0; i < inputVal; i++){ obj.push(selectVal) } } }); if (obj.length > 0) console.log(obj) })
(默认值)选择最紧凑的表示形式。
答案 1 :(得分:2)
在行之间读取,我认为输入格式可能是单个JSON数组,并且所需的输出是该数组元素的换行符分隔的JSON表示。如果是这样,这可能就是所有需要的:
with open('testnoline.json', 'w') as outfile:
for obj in data_json:
outfile.write(json.dumps(obj) + "\n")