Search code examples
pythondata-processing

Read json file as input and output as pprint?


I'm working with a large json file that is currently encoded as one long line.

This makes it unintelligable for other people to work with, so I want to render it using pprint.

At the moment I'm trying to import the full file and print as pprint but my output looks like this:

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

My question is- what is that showing? How can I get it to output the json data as pprint?

The code I've written looks like this:

import pprint

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

    pprint.pprint(input_data_file)

Solution

  • You opened the file in read mode but forgot to read the file contents.

    Just change pprint.pprint(input_data_file) with pprint.pprint(input_data_file.read()) and voila!