0

I have a json-text file containing tweets from a certain hashtag. 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.

2
  • SO no es un servicio de revisión de código. Tu programa falla en varios sitios con errores en tiempo de ejecución. Es mejor mostrar respeto por el tiempo de la gente que te puede ayudar de manera gratuita y desinteresada. Antes de postear una pregunta debes isolar el problema y preguntar por lo que falla: no es correcto vomitar todo el codigo y todos los datos y decir "no funcioina". Te envío un enlace que habla sobre este tema: stackoverflow.com/help/mcve Suerte con tu estudio de twitter. Commented Nov 12, 2015 at 10:58
  • ¡Muchas gracias! Tranquilo tienes toda la razón en que tengo que ser mucho más específico. Y aunque llevo un tiempecillo por aquí, no soy programador, aunque intento aprender a serlo. Commented Nov 12, 2015 at 11:04

1 Answer 1

1

In your code, tweets is a tuple:

'tuple' object has no attribute '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]
...
Sign up to request clarification or add additional context in comments.

2 Comments

Yes, you are right. At the beginning tweets was a list, then I changed it in my effort to find the bug. But as you can observe I did not.
¡¡¡Genial!!! Mil gracias. Así que el problema residía en cómo abrir el archivo y no en la declaración de variables...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.