python - Trying to parse twitter json from a text file -


i new python , trying parse "tweets' text file analysis.

my test file has number of tweets, here example of one:

{"created_at":"mon may 06 17:39:59 +0000 2013","id":331463367074148352,"id_str":"331463367074148352","text":"extra\u00f1o el trabajo en las aulas !! * se jala los cabellos","source":"\u003ca href=\"http:\/\/twitter.com\/download\/iphone\" rel=\"nofollow\"\u003etwitter iphone\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":276765971,"id_str":"276765971","name":"shiro","screen_name":"_shira3mmanuel_","location":"","url":null,"description":null,"protected":false,"followers_count":826,"friends_count":1080,"listed_count":5,"created_at":"mon apr 04 01:36:52 +0000 2011","favourites_count":1043,"utc_offset":-21600,"time_zone":"mexico city","geo_enabled":true,"verified":false,"statuses_count":28727,"lang":"es","contributors_enabled":false,"is_translator":false,"profile_background_color":"1a1b1f","profile_background_image_url":"http:\/\/a0.twimg.com\/images\/themes\/theme9\/bg.gif","profile_background_image_url_https":"https:\/\/si0.twimg.com\/images\/themes\/theme9\/bg.gif","profile_background_tile":false,"profile_image_url":"http:\/\/a0.twimg.com\/profile_images\/3608152674\/45133759fb72090ebbe880145d8966a6_normal.jpeg","profile_image_url_https":"https:\/\/si0.twimg.com\/profile_images\/3608152674\/45133759fb72090ebbe880145d8966a6_normal.jpeg","profile_banner_url":"https:\/\/si0.twimg.com\/profile_banners\/276765971\/1367525440","profile_link_color":"2fc2ef","profile_sidebar_border_color":"181a1e","profile_sidebar_fill_color":"252429","profile_text_color":"666666","profile_use_background_image":true,"default_profile":false,"default_profile_image":false,"following":null,"follow_request_sent":null,"notifications":null},"geo":{"type":"point","coordinates":[19.30303082,-99.54709768]},"coordinates":{"type":"point","coordinates":[-99.54709768,19.30303082]},"place":{"id":"1d23a12800a574a8","url":"http:\/\/api.twitter.com\/1\/geo\/id\/1d23a12800a574a8.json","place_type":"city","name":"lerma","full_name":"lerma, m\u00e9xico","country_code":"mx","country":"m\u00e9xico","bounding_box":{"type":"polygon","coordinates":[[[-99.552193,19.223171],[-99.552193,19.4343],[-99.379483,19.4343],[-99.379483,19.223171]]]},"attributes":{}},"contributors":null,"retweet_count":0,"favorite_count":0,"entities":{"hashtags":[],"symbols":[],"urls":[],"user_mentions":[]},"favorited":false,"retweeted":false,"filter_level":"medium","lang":"es"} 

my code is:

import re  import json    pattern_split = re.compile(r"\w+")   def sentment_tbl(sent_file):     # read in afinn-111.txt     tbl = dict(map(lambda (w, s): (w, int(s)), [     ws.strip().split('\t') ws in open(sent_file)]))     return tbl  def sentiment(text,afinn):     # word splitter pattern      words = pattern_split.split(text.lower())     sentiments = map(lambda word: afinn.get(word, 0), words)     if sentiments:         sentiment = float(sum(sentiments))     else:         sentiment = 0     return sentiment  def main():      sent_file = sys.argv[1]     afinn = sentment_tbl(sent_file)      tweet_file = (sys.argv[2])     open(tweet_file) f:        line_str in f:         print type(line_str)         print line_str         tweet = json.loads(line_str.read())         print("%6.2f %s" % (sentiment(line_str,afinn)))       #test: text = "finn stupid , idiotic"     #print("%6.2f %s" % (sentiment(text,afinn), text))   if __name__ == '__main__':     main() 

i error

i feeling mixing apples , oranges , experienced assistance

thanks, chris

if you've written multiple tweets file. eg:

 o.write(tweet1)  o.write(tweet2) 

you have read them in line line well, because json can't decode file of multiple objects written line line.

tweets = [] line in open('test.txt', 'r'):     tweets.append(json.loads(line)) 

Comments

Popular posts from this blog

java - Jmockit String final length method mocking Issue -

asp.net - Razor Page Hosted on IIS 6 Fails Every Morning -

c++ - wxwidget compiling on windows command prompt -