Python guarda el archivo a csv
Tengo el siguiente código que aparece en los tweets de Twitter y debo procesar los datos y luego guardarlos en un nuevo archivo.
Este es el código:
#import regex
import re
#start process_tweet
def processTweet(tweet):
# process the tweets
#Convert to lower case
tweet = tweet.lower()
#Convert www.* or https?://* to URL
tweet = re.sub('((www\.[\s]+)|(https?://[^\s]+))','URL',tweet)
#Convert @username to AT_USER
tweet = re.sub('@[^\s]+','AT_USER',tweet)
#Remove additional white spaces
tweet = re.sub('[\s]+', ' ', tweet)
#Replace #word with word
tweet = re.sub(r'#([^\s]+)', r'\1', tweet)
#trim
tweet = tweet.strip('\'"')
return tweet
#end
#Read the tweets one by one and process it
input = open('withoutEmptylines.csv', 'rb')
output = open('editedTweets.csv','wb')
line = input.readline()
while line:
processedTweet = processTweet(line)
print (processedTweet)
output.write(processedTweet)
line = input.readline()
input.close()
output.close()
Mis datos en el archivo de entrada se ven así, por lo que cada tweet en una línea:
She wants to ride my BMW the go for a ride in my BMW lol http://t.co/FeoNg48AQZ
BMW Sees U.S. As Top Market For 2015 i8 http://t.co/kkFyiBDcaP
mi función funciona bien, pero no estoy satisfecho con la salida que se ve así:
she wants to ride my bmw the go for a ride in my bmw lol URL rt AT_USER Ðun bmw es mucho? yo: bmw. -AT_USER veeergaaa!. hahahahahahahahaha nos hiciste la noche caray!
así que pone todo en una fila y no cada tweet en una fila como lo fue el formato en el archivo de entrada.
¿Alguien tiene una idea para obtener cada tweet en una línea?