如何阅读twitter-json文件并将其保存为带有Python的csv

时间:2015-11-12 10:23:38

标签: python json csv twitter

我有a json-text file containing tweets from a certain hashtag。现在我将它转换为矩阵,每个推文都有一行,以及一系列列,如用户,时间,纬度,经度等。我写了下面的代码,但是当我得到输出文件时,信息没有保存。它刚刚显示了标题行:

#import module
import json
from csv import writer

#input file
tweets = ()
for line in open('file.txt'):
    try: 
        tweets.append(json.loads(line))
    except:
        pass

#variables
ids = [tweet['id_str'] for tweet in tweets]
times = [tweet['created_at'] for tweet in tweets]
users = [tweet['user']['name'] for tweet in tweets]
texts = [tweet['text'] for tweet in tweets]
lats = [(T['geo']['coordinates'][0] if T['geo'] else None) for T in tweets]
lons = [(T['geo']['coordinates'][1] if T['geo'] else None) for T in tweets]
place_names = [(T['place']['full_name'] if T['place'] else None) for T in tweets]
place_types = [(T['place']['place_type'] if T['place'] else None) for T in tweets]

#output file
out = open('tweets_file.csv', 'w')
print >> out, 'id,created,text,user,lat,lon,place name,place type'
rows = zip(ids, times, texts, users, lats, lons, place_names, place_types)
csv = writer(out)
for row in rows:
    values = [(value.encode('utf8') if hasattr(value, 'encode') else value) for value in row]
    csv.writerow(values)
out.close()

请你能帮我找到并清除这个错误......提前致谢。

R上。

1 个答案:

答案 0 :(得分:1)

在您的代码中,推文是tuple

  

'tuple'对象没有属性'append'

似乎你有来自多个来源的复制粘贴代码而不了解正在做什么。

import json
from csv import writer

with open('file.txt') as data_file:    
    data = json.load(data_file)

tweets = data['statuses']

ids = [tweet['id_str'] for tweet in tweets]
times = [tweet['created_at'] for tweet in tweets]
...