2013-05-08 7 views
3

Я новичок в Python и я пытаюсь разобрать «твиты из текстового файла для анализа.Пытаясь разобрать твиттер JSON из текстового файла

Мой тестовый файл имеет несколько твитов, вот пример один:

{"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 for 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"} 

Мой код:

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') for 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]) 
    with open(tweet_file) as f: 
     for 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 is stupid and idiotic" 
    #print("%6.2f %s" % (sentiment(text,afinn), text)) 


if __name__ == '__main__': 
    main() 

Я получаю ошибку о

Я получаю чувство Я смешивание яблок и апельсинов, и хотел бы некоторым опытные

поддержку всех

спасибо, Крис

+4

Вы следуете за курсом Введение в науку о данных? :) –

+0

Очень вероятно ..: D – Alex

ответ

0

Вам нужно передать строку в json.loads:

tweet = json.loads(line_str) 

, как line_str строка.

После этого вам необходимо убедиться, что вы надлежащим образом прошли tweet или некоторые детали в tweet до sentiment() для дальнейшей обработки. Обратите внимание, что вы сейчас звоните sentiment() с line_str и tweet не используется (пока).

1

Почему бы вам не использовать встроенный JSON library вместо вашего цикла, чтение и разбор каждой строки в виде JSON, следующим образом:

import json 
jsonObj = json.loads(open(tweet_file, 'r')) 
# Now jsonObject is an array of dictionaries corresponding to the JSON 
Смежные вопросы