【问题标题】:Read json file as input and output as pprint? [duplicate]读取json文件作为输入输出为pprint? [复制]
【发布时间】:2017-12-05 09:28:26
【问题描述】:

我正在处理一个大型 json 文件,该文件当前被编码为一长行。

这让其他人难以理解,所以我想使用 pprint 来渲染它。

目前我正在尝试导入完整文件并打印为pprint,但我的输出如下所示:

<_io.TextIOWrapper name='hash_mention.json' mode='r' encoding='UTF-8'>

我的问题是——这显示了什么?如何让它将 json 数据输出为 pprint?

我写的代码是这样的:

import pprint

with open('./hash_mention.json', 'r') as input_data_file:

    pprint.pprint(input_data_file)

【问题讨论】:

  • pprint.pprint(input_data_file) --> pprint.pprint(input_data_file.read())
  • 你的操作系统是什么?
  • 我正在使用 MacOs
  • 你可以使用this 回答... HTH

标签: python data-processing


【解决方案1】:

您以读取模式打开文件,但忘记读取文件内容。

只需将pprint.pprint(input_data_file) 更改为pprint.pprint(input_data_file.read()),瞧!

【讨论】:

    猜你喜欢
    • 2022-01-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多