2016-04-25 23 views
7

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)

risposta

6

In questo caso è possibile evitare le espressioni regolari. Le righe del testo che hai presentato sono coerenti in termini di quanti spazi precedono il testo del tweet. Basta split():

>>> data = """ 
    lines with tweets here 
""" 
>>> for line in data.splitlines(): 
...  print(line.split(" ", 7)[-1]) 
... 
rt  we're treating one of you lads to this d'struct denim shirt! simply follow & rt to enter 
this album is wonderful, i'm so proud of you, i loved this album, it really is the best. -273 
international break is garbage smh. it's boring and your players get injured 
get weather updates from the weather channel. 15:27:19 
woah what happened to twitter this update is horrible 
i've completed the daily quest in paradise island 2! 
new post: henderson memorial public library 
who's going to next week? 
why so blue? @ golden bee 
+0

Cosa sta facendo [-1]? Impostare l'indice poco prima dello spazio? –

+2

@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

+3

@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

0

Il modello che si sta cercando è .+ \d+:

import re 
p = re.compile(".+ \d+") 
tweets = p.sub('', data) # data is the original string 

Ripartizione del Pattern

. corrisponde a qualsiasi carattere, e + partite 1 o più. Quindi, .+ corrisponde a uno o più caratteri. Tuttavia, se lo lasciassimo in questo modo, rimuoveremmo tutto il testo.

Quindi, vogliamo terminare il modello con \d+ - \d corrisponde a qualsiasi cifra, e quindi questo corrisponderebbe a qualsiasi sequenza continua di cifre, l'ultima delle quali sono gli ID tweet.

+1

Controllerà questo e ritornerà a voi. –

+1

Il tuo modello non funziona per questa linea: 'Ven 13 Nov 20:27:20 +0000 2015 3168571911 ricevi aggiornamenti meteo dal canale meteo. 15: 27: 19'. Visualizza ': 27: 19'. – cromod

2

È possibile farlo senza un'espressione regolare

import glob 

for filename in glob.glob('file.txt'): 
    with open("plain text - preprocesshurricane.txt",'a') as outfile ,open(filename, 'r') as infile: 
     for tweet in infile.readlines(): 
      temp=tweet.split(' ') 
      print('{}'.format(' '.join(temp[7:]))) 
+0

Questo è di nuovo uscita indesiderata, credo. Questo include FRI NOV? Ma ora mi rendo conto che ho semplicemente dovuto rompere la spaccatura e unirmi dopo il settimo spazio. Grazie per la tua risposta. –

+1

ho fatto la mia modifica ... – danidee

1

propongo un po 'modello più specifico di @Rushy Panchal per evitare problemi quando tweets sono le cifre: .+ \+(\d+){3}

funzione Use re.sub

>>> import re 
>>> with open('your_file.txt','r') as file: 
...  data = file.read() 
...  print re.sub('.+ \+(\d+){3}','',data) 

uscita

rt  we're treating one of you lads to this d'struct denim shirt! simply follow & rt to enter 
this album is wonderful, i'm so proud of you, i loved this album, it really is the best. -273 
international break is garbage smh. it's boring and your players get injured 
get weather updates from the weather channel. 15:27:19 
woah what happened to twitter this update is horrible 
i've completed the daily quest in paradise island 2! 
new post: henderson memorial public library 
who's going to next week? 
why so blue? @ golden bee 
Problemi correlati