0

I'm running twitter_hashtag_frequency.py program on Pycharm with json file parameter and I have this error :

['C:/Users/HP/PycharmProjects/Bonzanini_Book_Exercises/twitter_hashtag_frequency.py', 'stream_.jsonl']
Traceback (most recent call last):

File "C:/Users/HP/PycharmProjects/Bonzanini_Book_Exercises/twitter_hashtag_frequency.py", line 17, in <module>
    tweet = json.loads(line)
  File "C:\Users\HP\Anaconda3\lib\json\__init__.py", line 348, in loads
    return _default_decoder.decode(s)
  File "C:\Users\HP\Anaconda3\lib\json\decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "C:\Users\HP\Anaconda3\lib\json\decoder.py", line 355, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 2 column 1 (char 1)

This is the code:

# Chap02-03/twitter_hashtag_frequency.py
import sys
from collections import Counter
import json

def get_hashtags(tweet):
    entities = tweet.get('entities', {})
    hashtags = entities.get('hashtags', [])
    return [tag['text'].lower() for tag in hashtags]

if __name__ == '__main__':
    print(sys.argv)
    fname = sys.argv[1]
    with open(fname, 'r') as f:
        hashtags = Counter()
        for line in f:
            tweet = json.loads(line)
            hashtags_in_tweet = get_hashtags(tweet)
            hashtags.update(hashtags_in_tweet)
        for tag, count in hashtags.most_common(20):
            print("{}: {}".format(tag, count))

As you see in the screenshot the json file stream_.jsonlis in the same path with the program Pycharm running window

The windows of edit configuration sounds like good this is the screenshoot Run debug configurations screenshot

The json file file is an output of streaming tweets of 4629 lines. I would have your help, thank you.

Ado
  • 31
  • 5

0 Answers0