2016-03-17 68 views
0

我正在尝试使用python运行twitter情感分析。我从来没有用过python,所以对它很新。当我输入命令时,我收到很多错误,有些提到解码器。我不知道这是否是Python代码或某个配置问题中的问题。我的Python代码是:Python JSON解码错误

import sys 
import json 


def calc_sentiment(): 
    print 'Hello, world!' 

def lines(fp): 
    print str(len(fp.readlines())) 

def main(): 
    sent_file = open(sys.argv[1]) 
    scores = {} #Make empty dictionary 

    for line in sent_file: 
     term,score=line.split("\t") 
     scores[term] = int(score) #COnverting score to integer 

    #print scores.items() #display contents of scores 

    data = [] 
    sentiment = 0.0 
    with open(sys.argv[2]) as f: 
     for line in f: 
      data.append(json.loads(line)) 

    for index,item in enumerate(data): 
     sentiment = 0.0 
     tweet_dict = data[index] 
     if 'text' in tweet_dict: 
      tweet_txt = tweet_dict['text'] 
      #tweet_txt = data[index]['text'] 
      tweet_list = tweet_txt.split() 
      for word in tweet_list: 
       if word in scores: 
        sentiment = sentiment + scores[word] 
     print sentiment 


if __name__ == '__main__': 
    main() 

我收到的错误是:

Traceback (most recent call last): 
    File "tweet_sentiment.py", line 41, in <module> 
    main() 
    File "tweet_sentiment.py", line 25, in main 
    data.append(json.loads(line)) 
    File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/__init__.py", line 338, in loads 
    return _default_decoder.decode(s) 
    File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/decoder.py", line 366, in decode 
    obj, end = self.raw_decode(s, idx=_w(s, 0).end()) 
    File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/decoder.py", line 382, in raw_decode 
    obj, end = self.scan_once(s, idx) 
ValueError: Expecting , delimiter: line 1 column 204 (char 203) 
Chat Conversation End 

,我使用的命令是:蟒蛇tweet_sentiment.py AFINN-111.txt Citibank.txt

Citibank.txt包含所有推文,AFINN-111.txt包含单词分数。

如果任何人有任何想法我做错了什么或我如何解决它我真的很感激的建议。 Citibank.txt的

内容是:

{"statuses":[{"metadata":{"iso_language_code":"en","result_type":"recent"},"created_at":"Fri Mar 11 16:09:43 +0000 2016","id":708324032152674304,"id_str":"708324032152674304","text":"I just checked in at Citibank with #mPLUSPlaces Download today! https:\/\/t.co\/utoW3FFs4R","source":"\u003ca href=\"http:\/\/www.apple.com\" rel=\"nofollow\"\u003eiOS\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":2893172851,"id_str":"2893172851","name":"Virginia","screen_name":"virginia62610","location":"","description":"","url":null,"entities":{"description":{"urls":[]}},"protected":false,"followers_count":1,"friends_count":2,"listed_count":0,"created_at":"Fri Nov 07 21:00:38 +0000 2014","favourites_count":0,"utc_offset":null,"time_zone":null,"geo_enabled":true,"verified":false,"statuses_count":2730,"lang":"en","contributors_enabled":false,"is_translator":false,"is_translation_enabled":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_image_url":"http:\/\/abs.twimg.com\/sticky\/default_profile_images\/default_profile_2_normal.png","profile_image_url_https":"https:\/\/abs.twimg.com\/sticky\/default_profile_images\/default_profile_2_normal.png","profile_link_color":"0084B4","profile_sidebar_border_color":"C0DEED","profile_sidebar_fill_color":"DDEEF6","profile_text_color":"333333","profile_use_background_image":true,"has_extended_profile":false,"default_profile":true,"default_profile_image":true,"following":false,"follow_request_sent":false,"notifications":false},"geo":null,"coordinates":null,"place":null,"contributors":null,"is_quote_status":false,"retweet_count":0,"favorite_count":0,"entities":{"hashtags":[{"text":"mPLUSPlaces","indices":[35,47]}],"symbols":[],"user_mentions":[],"urls":[{"url":"https:\/\/t.co\/utoW3FFs4R","expanded_url":"http:\/\/bit.ly\/gomplaces","display_url":"bit.ly\/gomplaces","indices":[65,88]}]},"favorited":false,"retweeted":false,"possibly_sensitive":false,"lang":"en"},{"metadata":{"iso_language_code":"en","result_type":"recent"},"created_at":"Fri Mar 11 16:09:05 +0000 2016","id":708323871909289985,"id_str":"708323871909289985","text":"I just checked in at Citibank with #mPLUSPlaces Download today! https:\/\/t.co\/HQQWGhDBRB","source":"\u003ca href=\"http:\/\/www.apple.com\" rel=\"nofollow\"\u003eiOS\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":25390853,"id_str":"25390853","name":"Noelle Arzillo","screen_name":"noellemouse","location":"","description":"","url":null,"entities":{"description":{"urls":[]}},"protected":false,"followers_count":20,"friends_count":13,"listed_count":0,"created_at":"Thu Mar 19 21:35:30 +0000 2009","favourites_count":2,"utc_offset":null,"time_zone":null,"geo_enabled":false,"verified":false,"statuses_count":863,"lang":"en","contributors_enabled":false,"is_translator":false,"is_translation_enabled":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_image_url":"http:\/\/pbs.twimg.com\/profile_images\/103959403\/n832189172_575796_9750_normal.jpg","profile_image_url_https":"https:\/\/pbs.twimg.com\/profile_images\/103959403\/n832189172_575796_9750_normal.jpg","profile_link_color":"0084B4","profile_sidebar_border_color":"C0DEED","profile_sidebar_fill_color":"DDEEF6","profile_text_color":"333333","profile_use_background_image":true,"has_extended_profile":false,"default_profile":true,"default_profile_image":false,"following":false,"follow_request_sent":false,"notifications":false},"geo":null,"coordinates":null,"place":null,"contributors":null,"is_quote_status":false,"retweet_count":0,"favorite_count":0,"entities":{"hashtags":[{"text":"mPLUSPlaces","indices":[35,47]}],"symbols":[],"user_mentions":[],"urls":[{"url":"https:\/\/t.co\/HQQWGhDBRB","expanded_url":"http:\/\/bit.ly\/gomplaces","display_url":"bit.ly\/gomplaces","indices":[65,88]}]},"favorited":false,"retweeted":false,"possibly_sensitive":false,"lang":"en"},{"metadata":{"iso_language_code":"en","result_type":"recent"},"created_at":"Fri Mar 11 16:07:54 +0000 2016","id":708323572385718273,"id_str":"708323572385718273","text":"RT @TODAYshow: Monday on [email protected] is live in concert on the plaza! #ColdplayTODAY https:\/\/t.co\/7ChpiWusxk","source":"\u003ca href=\"http:\/\/twitter.com\" rel=\"nofollow\"\u003eTwitter Web Client\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":80374332,"id_str":"80374332","name":"Citibank","screen_name":"Citibank","location":"","description":"Special offers, ideas for managing your financial life, and other updates from Citibank. Looking for customer service? Connect with @AskCiti","url":"http:\/\/t.co\/K2nfvcI2Bf","entities":{"url":{"urls":[{"url":"http:\/\/t.co\/K2nfvcI2Bf","expanded_url":"http:\/\/www.citi.com","display_url":"citi.com","indices":[0,22]}]},"description":{"urls":[]}},"protected":false,"followers_count":326521,"friends_count":36638,"listed_count":1084,"created_at":"Tue Oct 06 18:49:45 +0000 2009","favourites_count":1107,"utc_offset":-18000,"time_zone":"Eastern Time (US & Canada)","geo_enabled":false,"verified":true," 
+0

如果你在通过'json.loads'之前打印'line',它是什么样的? – JCVanHamme

+0

您发布的JSON无效。你无法提供JSON解析器无效的JSON,并期望它工作。 –

回答

1

您正在尝试解码由行文件中的行,但作为输入json.loads你应该提供一个str含有全正确的JSON。试着用

sentiment = 0.0 
with open(sys.argv[2]) as f: 
    data = json.load(f) 

修订

好了,问题来代替

data = [] 
sentiment = 0.0 
with open(sys.argv[2]) as f: 
     for line in f: 
      data.append(json.loads(line)) 

看起来像问题描述herethere。它可以部分地用

with open('Citibank.txt', 'r') as f: 
    for line in f: 
     l = line.replace("\\", r"\\") 
     data.append(json.loads(l)) 
+0

更改后仍然收到相同的错误。 –

+0

@ A.B您可以发布“Citibank.txt”文件(或其中的一部分)的内容吗? – kvorobiev

+0

已编辑文章的部分内容 –