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

标签 python json pretty-print linefeed

我正在从 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你的数据到 json:

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

引用文档:

If indent is a non-negative integer, then JSON array elements and object members will be pretty-printed with that indent level. An indent level of 0, or negative, will only insert newlines. None (the default) selects the most compact representation.

关于python - 使用 Python 删除 json 文件中的新换行符。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44794782/

相关文章:

python - 下载 pdf 循环

android - 将数据从 mysql 的 listview JSON 发送到新 Activity

PHP mysql 从 json 插入/更新

Scala - 绕过重写的 toString 方法

java - 最好的 Java pretty-print 库?

python - 为什么要将 flask webapp 作为一个包来安装?

python - SQLAlchemy:插入的刷新顺序错误?

python - www 域和 ip 地址的 django allowed_hosts 错误

javascript - 有没有办法阻止 JSON.stringify 调用 toJSON?

haskell - Haskell 中的 pretty-print : Break outer groups when printing nested tuples