0

I am using twitter and downloaded a sample code from:" https://stream.twitter.com/1.1/statuses/sample.json"

I used pretty printing but it doesn't print like how I want it.

I only need the user "name" or "screen_name", "user_mention", and "retweeted". I need this to draw a tree with nodes (names) and edges (retweets or mentions with sentimate value (+/-).

first: I dont know how to remove everything from json to just print the 3 things.

Code:


        with open (fname) as json_file:

        for line in json_file.readlines():

            type(line)

            f_contents = json_file.read()

            keywords = ["id","screen_name","retweeted", "user_mention" ]

            keywords =set(keywords)

            print(keywords)

            pprint.pprint(line, indent = 4 , width=5)

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
Fulla
  • 79
  • 7

2 Answers2

0

If you want to filter some dictionary by keys, there are some approaches. You can see the solutions discussed at this thread: https://stackoverflow.com/a/3420156/3921457

One solution in your case can be something like:

import json 

keywords = {'id','screen_name','retweeted', 'user_mention'}

with open(fname) as file:
    for raw_line in file.readlines():
        full_line = json.loads(line)
        line = {key: full_line[key] for key in keywords}

        pprint.pprint(line, indent = 4 , width=5)
garciparedes
  • 1,749
  • 2
  • 18
  • 34
0
  1. Use the json library to actually load the file as structured data. Trying to read the file a line at a time isn't going to work very well, because it ignores how JSON is structured; and the .read() call here ruins the strategy anyway (it reads the entire rest of the file aside from the first line, into f_contents, and then the loop doesn't run again after that).

So:

import json

with open(fname) as json_file:
    data = json.load(json_file)
  1. Use Python operations to pull out the parts of the data you need. What you will have is an ordinary dict or list that contains more dicts or lists, etc., as deeply nested as the JSON is.

  2. Now you can pprint the relevant fragments.

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153