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

2024-01-14

我正在从 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)

您只需要确保indent=None当你dump https://docs.python.org/2/library/json.html#basic-usage你的数据到json:

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

引用文档:

If indent是一个非负整数,那么 JSON 数组元素和对象成员将使用该缩进级别进行漂亮的打印。缩进级别为 0 或负数只会插入换行符。None(默认)选择最紧凑的表示。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 Python 删除 json 文件中的新换行符。 的相关文章

随机推荐