2016-04-25 5 views
7

Ich habe eine Reihe von Tweets in Klartextform, die unten gezeigt werden. Ich suche nur den Textteil zu extrahieren.Wie man eine Teilzeichenkette von der Textdatei in Python holt?

Beispieldaten in Datei -

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 

Dies ist mein Versuch im Vorprozess Bühne -

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 

Dieser Ausgang ist nicht die gewünschte Ausgabe weil

1. Es wird mir Zahlen/Ziffern im Textteil des tweet holen nicht zulassen.
2. Jede Zeile beginnt mit FRI NOV.

Könnten Sie bitte vorschlagen, eine bessere Methode, um das gleiche zu erreichen? Ich bin nicht vertraut mit Regex, aber ich nehme an, wir könnten re.search(r'2015(magic to remove tweetID)/w*',tweet)

Antwort

6

beschäftigen Sie können in diesem Fall regulären Ausdrücken vermeiden. Die Zeilen des Textes, die Sie dargestellt haben, sind konsistent hinsichtlich der Anzahl der Leerzeichen vor dem Tweet-Text. split() einfach:

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

Was macht [-1]? Den Index direkt vor dem Leerzeichen setzen? –

+2

@MayurH 'line.split (" ", 7)' teilt eine Zeile nach den ersten 7 Leerzeichen.Es erzeugt eine Liste, in der der Tweet-Text das letzte Element ist - wir erhalten es nach dem letzten Index. – alecxe

+3

@MayurH Der Index '-1' in' [-1] 'zeigt auf die letzte Position in' '(gibt IndexError auf eine leere Liste). Sie können ausgefallene Sachen wie ' [-3:]' machen, um eine Liste der letzten drei Elemente usw. zu erhalten. – quapka

0

Das Muster die Sie suchen, ist .+ \d+:

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

Aufteilung des Pattern

. jedes Zeichen und + Matches 1 oder mehr. So entspricht .+ einem oder mehreren Zeichen. Wenn wir es jedoch einfach hier belassen, würden wir den gesamten Text entfernen.

Also, wir wollen das Muster mit \d+ - \d entspricht jeder Ziffer, und damit würde dies jede kontinuierliche Folge von Ziffern übereinstimmen, die letzten davon sind die Tweet IDs.

+1

Wird dies überprüfen und zu Ihnen zurückkehren. –

+1

Ihr Muster funktioniert nicht für diese Zeile: 'Fri Nov 13 20:27:20 +0000 2015 3168571911 erhalten Wetter-Updates vom Wetterkanal. 15: 27: 19'. Sie zeigen ': 27: 19' an. – cromod

2

Sie können es tun, ohne einen regulären Ausdruck

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

Das ist wieder ein unerwünschter Ausgang, glaube ich. Dies beinhaltet FRI NOV? Aber ich merke jetzt, dass ich einfach die Spaltung brechen und mich nach dem 7. Platz anschließen musste. Danke für deine Antwort. –

+1

Ich habe meine Bearbeitung gemacht ... – danidee

1

ich ein wenig mehr spezifische Muster als @Rushy Panchal vorschlagen Probleme zu vermeiden, wenn Tweets Ziffern umfassen: .+ \+(\d+){3}

Verwenden re.sub Funktion

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

Ausgabe

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 
Verwandte Themen