2017-06-20 3 views
0

Ich möchte einige Informationen aus dem obigen TXT extrahieren. Datei (die als inf.txt bezeichnet wird), um einen Datenrahmen in Python zu erstellen. Einige Beispiele von inf.txt sind hier:Erstellen Sie einen Datenrahmen von txt

bene_id_18900 (Variable1, 43) 
bene_id_18900 (Variable4, 0) 
dtype: object 0 
encrypted 723 beneficiary id (Label1, 43) 
encrypted 723 beneficiary id (Label5, 4) 
dtype: object 0 
bene_id_18900 (Variable1, 43) 
bene_id_18900 (Variable4, 0) 
dtype: object 0 
from  (Variable4, 95) 
from   (VNAME4, 95) 
from  (Variable6, 94) 
from   (VNAME6, 94) 
dtype: object 2 
first day on claim billing statement  (Label4, 95) 
first day on claim billing statement  (Label6, 94) 
dtype: object 2 
thru  (Variable4, 140) 
thru  (VNAME4, 140) 
thru  (Variable6, 142) 
thru  (VNAME6, 142) 
dtype: object 3 
last day on claim billing statement  (Label4, 140) 
last day on claim billing statement  (Label6, 142) 
dtype: object 3 

Das will Datenrahmen ist hier:

1 2 3 4 5 6 
0 43 na na 0 4 na 
1 na na na na na na 
2 4 5 na 95 na 94 
3 na na na 140 na 142 

Die Zeilennummer von der Nummer folgenden dtype ist: Objekt, die Spaltennummer aus der zweiten Reihe ist in jeder Klammer.

Zum Beispiel, in der ersten Zeile ist es (Variable1, 43): es gehört zu dtype: Objekt 0, so dass es in der ersten Zeile; Variable1, also in der ersten Spalte.

Ein anderes Beispiel, in der vorletzten Zeile, es (Label6, 142): es gehört zu dtype: Objekt 3, also in der dritten Zeile; Label6, also in der sechsten Spalte.

Alle diese Zeichenfolge wie "bene_id_18900", "Variable", "Label" und so weiter sind eigentlich keine Bedeutung.

Meine Idee ist das Hinzufügen der entsprechenden Zeilennummer in jeder Klammer, so dass ich später alle nützlichen Informationen behalten und alle unbrauchbaren Informationen löschen kann. Wie folgt aus:

(1, 43, 0) 
(4, 0, 0) 
(1, 43, 0) 
(5, 4, 0) 
(1, 43, 0) 
(4, 0, 0) 
(4, 95, 1) 
(4, 95, 1) 
...... 
...... 
...... 

Mein Versuch, habe ich wirklich keine Ahnung ....

with open('/Users/xccxken/Dropbox/inf.txt') as f: 
    content = f.readlines() 
content = [x.strip() for x in content] 
for x in content: 

Antwort

0

lassen Sie uns sagen, Sie kennen die Anzahl der Zeilen (M) und Spalten (N) aus der Textdatei . Eine einfache Analyse, um die maximale dtype und max label (no) Variable (no) zu erhalten, wird diese Information erhalten. neben einer Reihe von

MxN erstellen
import re 
import pandas as pd 
# assuming that you have found the max no of rows M and max no of columns N. 
M = 4 
N = 6 
# create MxN list of lists with values 'na' 
x = ['na'] * N 
data = [] 
for i in range(M): 
    tmp = list(x) 
    data.append(tmp) 
index_x = -999 # fix for NameError 
# data = [x] * M; this does not work since lists are mutable objects 

with open('/Users/xccxken/Dropbox/inf.txt') as fh: 
    for line in fh: 
     line = line.strip() 
     if 'dtype' in line: 
      # get the x axis index 
      index_x = int(line.split(' ')[-1]) 
     if 'Label' in line: 
      # get y axis index 
      c = re.search('Label(\d), (\d+)', line) 
      index_y = int(c.groups()[0]) 
      # reduce index_y by 1 as the col names start with 1 and python list is 0 index 
      if index_y > 0: 
       index_y -= 1 
      # get value 
      value = int(c.groups()[1]) 
      if index_x >= 0: # fix the NameError and a logical bug 
       # populate the correct x,y location in the list of lists 
       data[index_x][index_y] = value 
     if 'Variable' in line: 
      c = re.search('Variable(\d), (\d+)', line) 
      index_y = int(c.groups()[0]) 
      value = int(c.groups()[1]) 
      if index_y > 0: 
       index_y -= 1 
      if index_x >= 0: # fix the NameError and a logical bug 
       data[index_x][index_y] = value 
# create the col names 
cols = range(1, N+1) 
# create the dataframe 
df = pd.DataFrame(data, columns=cols) 

hoffe, das hilft, diese für mich gearbeitet nahm ich dies als Beispiel:

dtype: object 0 
encrypted 723 beneficiary id (Label1, 43) 
encrypted 723 beneficiary id (Label5, 4) 
dtype: object 0 
bene_id_18900 (Variable1, 43) 
bene_id_18900 (Variable4, 0) 
dtype: object 0 
from  (Variable4, 95) 
from   (VNAME4, 95) 
from  (Variable6, 94) 
from   (VNAME6, 94) 
dtype: object 2 
first day on claim billing statement  (Label4, 95) 
first day on claim billing statement  (Label6, 94) 
dtype: object 2 
thru  (Variable4, 140) 
thru  (VNAME4, 140) 
thru  (Variable6, 142) 
thru  (VNAME6, 142) 
dtype: object 3 
last day on claim billing statement  (Label4, 140) 
last day on claim billing statement  (Label6, 142) 
dtype: object 3 

und der Ausgang ist:

1 2 3 4 5 6 
0 43 na na 95 4 94 
1 na na na na na na 
2 na na na 140 na 142 
3 na na na 140 na 142 

nur fyi, ich betrachte diese auch als gültige Daten:

dtype: object 0 
from  (Variable4, 95) # is valid 
from   (VNAME4, 95) 
from  (Variable6, 94) 
from   (VNAME6, 94) # is valid 
+0

vielen dank! Ich bin net zu Python, ich kopiere den Code, aber es gibt einen Fehler in Zeile 39, sagt NameError: Name 'index_x' ist nicht definiert, bitte sagen Sie mir, wie zu beheben. Vielen Dank! – kkjoe

+0

hi kkjoe, bitte überprüfe jetzt, es sollte funktionieren. Ich hatte index_x nicht initialisiert, daher gab es NameError (weil dtype nicht die erste Zeile war). –

Verwandte Themen