Ho un sacco di tweet in formato testo in chiaro che viene mostrato di seguito. Sto cercando di estrarre solo la parte di testo .Come recuperare una sottostringa dal file di testo in python?
DATI CAMPIONE IN FILE -
Fri Nov 13 20:27:16 +0000 2015 4181010297 rt we're treating one of you lads to this d'struct denim shirt! simply follow & rt to enter
Fri Nov 13 20:27:16 +0000 2015 2891325562 this album is wonderful, i'm so proud of you, i loved this album, it really is the best. -273
Fri Nov 13 20:27:19 +0000 2015 2347993701 international break is garbage smh. it's boring and your players get injured
Fri Nov 13 20:27:20 +0000 2015 3168571911 get weather updates from the weather channel. 15:27:19
Fri Nov 13 20:27:20 +0000 2015 2495101558 woah what happened to twitter this update is horrible
Fri Nov 13 20:27:19 +0000 2015 229544082 i've completed the daily quest in paradise island 2!
Fri Nov 13 20:27:17 +0000 2015 309233999 new post: henderson memorial public library
Fri Nov 13 20:27:21 +0000 2015 291806707 who's going to next week?
Fri Nov 13 20:27:19 +0000 2015 3031745900 why so blue? @ golden bee
Questo è il mio tentativo nella fase di pre-elaborazione -
for filename in glob.glob('*.txt'):
with open("plain text - preprocesshurricane.txt",'a') as outfile ,open(filename, 'r') as infile:
for tweet in infile.readlines():
temp=tweet.split(' ')
text=""
for i in temp:
x=str(i)
if x.isalpha() :
text += x + ' '
print(text)
output-
Fri Nov rt treating one of you lads to this denim simply follow rt to
Fri Nov this album is so proud of i loved this it really is the
Fri Nov international break is garbage boring and your players get
Fri Nov get weather updates from the weather
Fri Nov woah what happened to twitter this update is
Fri Nov completed the daily quest in paradise island
Fri Nov new henderson memorial public
Fri Nov going to next
Fri Nov why so golden
Questa uscita è non il risultato desiderato perché
1. non mi permette di prenderti numeri/cifre entro la parte di testo del tweet.
2. Ogni riga inizia con FRI NOV.
Potrebbe suggerire un metodo migliore per ottenere lo stesso? Non ho molta familiarità con regex, ma presumo che potremmo impiegare re.search(r'2015(magic to remove tweetID)/w*',tweet)
Cosa sta facendo [-1]? Impostare l'indice poco prima dello spazio? –
@MayurH 'line.split (" ", 7)' divide una linea per i primi 7 spazi.Produce un elenco in cui il testo tweet è l'ultimo elemento: lo otteniamo dall'ultimo indice. – alecxe
@MayurH L'indice '-1' in' [-1] 'punta all'ultima posizione in' '(dà IndexError su una lista vuota). Puoi fare cose fantastiche come ' [-3:]' per ottenere una lista degli ultimi tre elementi, ecc. –
quapka