简体   繁体   English

如何使用Python读取twitter-json文件并将其另存为csv

[英]How to read twitter-json file and save it as a csv with Python

I have a json-text file containing tweets from a certain hashtag . 我有一个json-text文件,其中包含来自某个主题标签的推文 Now I transform it to a matrix with a row for each tweet and an array of columns such as user, time, latitude, longitude and so on. 现在,我将其转换为一个矩阵,其中每个推文都带有一行,并包含用户,时间,纬度,经度等列的数组。 I have written the following code, but when I get the output file the information is not saved. 我已经编写了以下代码,但是当我获得输出文件时,信息不会保存。 It has just showed the header row: 它刚刚显示了标题行:

#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()

Please could you help me to find and clear the bug... Thanks in advance. 请您能帮我找到并清除错误吗?

R. R.

In your code, tweets is a tuple : 在您的代码中,tweets是一个tuple

'tuple' object has no attribute 'append' 'tuple'对象没有属性'append'

It seems you have copy-paste code from several sources without understand what is doing. 看来您有几个来源的复制粘贴代码,但不了解正在做什么。

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]
...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM