首页 > 解决方案 > 在线命令运行python程序的问题

问题描述

我正在twitter_hashtag_frequency.py使用 json 文件作为参数运行 programm on line 命令,test.jsonl但我仍然有以下错误,但是我验证了这个 json 文件没有格式问题。

C:\Users\HP\PycharmProjects\Bonzanini_Book_Exercises>python twitter_hashtag_frequency.py test.jsonl

    Traceback (most recent call last):
      File "twitter_hashtag_frequency.py", line 18, in <module>
        tweet = json.loads(line)
      File "C:\Users\HP\Python\Python38\lib\json\__init__.py", line 357, in loads
        return _default_decoder.decode(s)
      File "C:\Users\HP\Python\Python38\lib\json\decoder.py", line 337, in decode
        obj, end = self.raw_decode(s, idx=_w(s, 0).end())
      File "C:\Users\HP\Python\Python38\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) 

这是的内容test.jsonl

{"created_at":"Tue Jul 21 00:47:40 +0000 2020","id":1285375860199972866,"id_str":"1285375860199972866","text":"RT @CBCAlerts: Big spike in new cases of COVID-19 in B.C., with 102 confirmed over weekend. 'We do have the possibility of having explosive\u2026","source":"\u003ca href=\"http:\/\/twitter.com\/download\/android\" rel=\"nofollow\"\u003eTwitter for Android\u003c\/a\u003e","truncated":false,"in_reply_to_status_id":null,"in_reply_to_status_id_str":null,"in_reply_to_user_id":null,"in_reply_to_user_id_str":null,"in_reply_to_screen_name":null,"user":{"id":2564198800,"id_str":"2564198800","name":"Fayesella","screen_name":"frbaerwald","location":null,"url":null,"description":null,"translator_type":"none","protected":false,"verified":false,"followers_count":6,"friends_count":107,"listed_count":0,"favourites_count":1228,"statuses_count":88,"created_at":"Sun May 25 21:51:11 +0000 2014","utc_offset":null,"time_zone":null,"geo_enabled":false,"lang":null,"contributors_enabled":false,"is_translator":false,"profile_background_color":"C0DEED","profile_background_image_url":"http:\/\/abs.twimg.com\/images\/themes\/theme1\/bg.png","profile_background_image_url_https":"https:\/\/abs.twimg.com\/images\/themes\/theme1\/bg.png","profile_background_tile":false,"profile_link_color":"1DA1F2","profile_sidebar_border_color":"C0DEED","profile_sidebar_fill_color":"DDEEF6","profile_text_color":"333333","profile_use_background_image":true,"profile_image_url":"http:\/\/pbs.twimg.com\/profile_images\/1249529611760717826\/pmKLZKkR_normal.jpg","profile_image_url_https":"https:\/\/pbs.twimg.com\/profile_images\/1249529611760717826\/pmKLZKkR_normal.jpg","default_profile":true,"default_profile_image":false,"following":null,"follow_request_sent":null,"notifications":null},"geo":null,"coordinates":null,"place":null,"contributors":null,"retweeted_status":{"created_at":"Mon Jul 20 22:26:19 +0000 2020","id":1285340287104147457,"id_str":"1285340287104147457","text":"Big spike in new cases of COVID-19 in B.C., with 102 confirmed over weekend. 'We do have the possibility of having\u2026 https:\/\/t.co\/X9JH7qNj6o","source":"\u003ca href=\"https:\/\/mobile.twitter.com\" rel=\"nofollow\"\u003eTwitter Web App\u003c\/a\u003e","truncated":true,"in_reply_to_status_id":null,"in_reply_to_status_id_str":null,"in_reply_to_user_id":null,"in_reply_to_user_id_str":null,"in_reply_to_screen_name":null,"user":{"id":19038934,"id_str":"19038934","name":"CBC News Alerts","screen_name":"CBCAlerts","location":"Toronto","url":"http:\/\/www.cbc.ca\/news\/","description":"Breaking national and international news alerts from CBC News, Canada's TV, radio, online and social media news leader.","translator_type":"none","protected":false,"verified":true,"followers_count":1304745,"friends_count":398,"listed_count":8806,"favourites_count":0,"statuses_count":142466,"created_at":"Thu Jan 15 21:03:19 +0000 2009","utc_offset":null,"time_zone":null,"geo_enabled":false,"lang":null,"contributors_enabled":false,"is_translator":false,"profile_background_color":"000000","profile_background_image_url":"http:\/\/abs.twimg.com\/images\/themes\/theme7\/bg.gif","profile_background_image_url_https":"https:\/\/abs.twimg.com\/images\/themes\/theme7\/bg.gif","profile_background_tile":false,"profile_link_color":"FF0000","profile_sidebar_border_color":"F2E195","profile_sidebar_fill_color":"FFF7CC","profile_text_color":"0C3E53","profile_use_background_image":true,"profile_image_url":"http:\/\/pbs.twimg.com\/profile_images\/563807705530245120\/92toBEKN_normal.jpeg","profile_image_url_https":"https:\/\/pbs.twimg.com\/profile_images\/563807705530245120\/92toBEKN_normal.jpeg","default_profile":false,"default_profile_image":false,"following":null,"follow_request_sent":null,"notifications":null},"geo":null,"coordinates":null,"place":null,"contributors":null,"is_quote_status":false,"extended_tweet":{"full_text":"Big spike in new cases of COVID-19 in B.C., with 102 confirmed over weekend. 'We do have the possibility of having explosive growth here in our outbreak, if we're not careful,' Provincial Health Officer Dr. Bonnie Henry said. https:\/\/t.co\/dg1t2Q7MZU","display_text_range":[0,249],"entities":{"hashtags":[],"urls":[{"url":"https:\/\/t.co\/dg1t2Q7MZU","expanded_url":"http:\/\/cbc.ca\/1.5655625","display_url":"cbc.ca\/1.5655625","indices":[226,249]}],"user_mentions":[],"symbols":[]}},"quote_count":48,"reply_count":26,"retweet_count":144,"favorite_count":208,"entities":{"hashtags":[],"urls":[{"url":"https:\/\/t.co\/X9JH7qNj6o","expanded_url":"https:\/\/twitter.com\/i\/web\/status\/1285340287104147457","display_url":"twitter.com\/i\/web\/status\/1\u2026","indices":[116,139]}],"user_mentions":[],"symbols":[]},"favorited":false,"retweeted":false,"possibly_sensitive":false,"filter_level":"low","lang":"en"},"is_quote_status":false,"quote_count":0,"reply_count":0,"retweet_count":0,"favorite_count":0,"entities":{"hashtags":[],"urls":[],"user_mentions":[{"screen_name":"CBCAlerts","name":"CBC News Alerts","id":19038934,"id_str":"19038934","indices":[3,13]}],"symbols":[]},"favorited":false,"retweeted":false,"filter_level":"low","lang":"en","timestamp_ms":"1595292460857"}

这是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__':
    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))

有人可以帮我解决这个问题吗?也许我不知道的代码有问题。我会很感激你的帮助,我有这个问题已经好几天了。

标签: pythonwindows

解决方案


推荐阅读