简体   繁体   English

使用Python在json文件中删除换行符。

[英]Removing a new line feed in json file using Python.

I am in the process of downloading data from firebase, exporting it into a json. 我正在从Firebase下载数据,并将其导出到json。 After this I am trying to upload it into bigquery but I need to remove the new line feed for big query to accept it. 在此之后,我尝试将其上传到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"}

This is how I need it but can not figure out how to do this besides manually doing it. 这就是我需要的方式,但是除了手动执行之外,我一无所知。

{"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"}

I am using python to load the json, read it and then write a new one but can not figure out the right code. 我正在使用python加载json,读取它,然后编写一个新的json,但找不到正确的代码。 Thank you! 谢谢!

here is the outline for my python code 这是我的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)

You only need to make sure that indent=None when you dump you data to json: dump数据dump到json时,只需确保indent=None即可:

with open('testnoline.json', 'w') as outfile:   
    json.dump(data_json, outfile, indent=None)

Quoting from the doc: 从文档中引用:

If indent is a non-negative integer, then JSON array elements and object members will be pretty-printed with that indent level. 如果indent是非负整数,则JSON数组元素和对象成员将使用该缩进级别进行漂亮打印。 An indent level of 0, or negative, will only insert newlines. 缩进级别0或负数将仅插入换行符。 None (the default) selects the most compact representation. None (默认)选择最紧凑的表示形式。

Reading between the lines, I think the input format might be a single JSON array, and the desired output is newline-separated JSON representations of the elements of that array. 在两行之间阅读时,我认为输入格式可能是单个JSON数组,并且所需的输出是该数组元素的以换行符分隔的JSON表示形式。 If so, this is probably all that's needed: 如果是这样,这可能就是所需的一切:

with open('testnoline.json', 'w') as outfile:
    for obj in data_json:
        outfile.write(json.dumps(obj) + "\n")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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