2017-06-07 2 views
2

zu erhalten Ich möchte schöne Suppe verwenden, um Tags zu finden, wo die Kind-Tags (Gewinne oder Verluste) größer als 0 sind. Ich würde dann gerne den Inhalt der inneren Tags drucken " Gewinne "" Verluste "und" band.textualrepresentation ". Dies ist im Wesentlichen das Skript, das ich möchte (obwohl dieses nicht funktioniert).Verwenden von schönen Suppe, um Klasseninhalte mit einer bedingten

import sys 
from BeautifulSoup import BeautifulSoup as Soup 

def parseLog(file): 
     file = sys.argv[1] 
     handler = open(file).read() 
     soup = Soup(handler) 
     for anytype in soup('anytype', 'gains'.string>0 || 'losses'.string>0): 
       gain = anytype.gains.string 
       loss = anytype.losses.string 
       band = anytype.band.textualrepresentation.string 
       print gain loss band 

parseLog(sys.argv[1]) 

ich in Schwierigkeiten frühzeitig leite, kann ich nicht einmal den Inhalt der Gewinne drucken, geschweige denn den Inhalt drucken, die ein bestimmten Kriterien erfüllen. Mein aktuelles Script

def parseLog(file): 
     file = sys.argv[1] 
     handler = open(file).read() 
     soup = Soup(handler) 
     for anytype in soup.findall('anytype'): 
       gain = anytype.fetch('gains') 
       print gain 

parseLog(sys.argv[1]) 

kehrt

Traceback (most recent call last): 
    File "./soup.py", line 13, in <module> 
    parseLog(sys.argv[1]) 
    File "./soup.py", line 9, in parseLog 
    for anytype in soup.findall('anytype'): 
TypeError: 'NoneType' object is not callable 

.

Probeneingangs

 <anytype xsi:type="GainLossStruct"> 
     <band> 
      <textualrepresentation> 
      22q11.1 
      </textualrepresentation> 
     </band> 
     <gains> 
      2 
     </gains> 
     <losses> 
      1 
     </losses> 
     <structs> 
      0 
     </structs> 
     </anytype> 
     <anytype xsi:type="GainLossStruct"> 
     <band> 
      <textualrepresentation> 
      22q11.2 
      </textualrepresentation> 
     </band> 
     <gains> 
      0 
     </gains> 
     <losses> 
      1 
     </losses> 
     <structs> 
      0 
     </structs> 
     </anytype> 
     <anytype xsi:type="GainLossStruct"> 
     <band> 
      <textualrepresentation> 
      22q12 
      </textualrepresentation> 
     </band> 
     <gains> 
      0 
     </gains> 
     <losses> 
      0 
     </losses> 
     <structs> 
      0 
     </structs> 
     </anytype> 

Beispielausgabe

2 1 22q11.1 
0 1 22q11.2 

.

.

aktualisieren Die aktuelle Lösung

import sys 
from BeautifulSoup import BeautifulSoup as Soup 

def parseLog(file): 
     file = sys.argv[1] 
     handler = open(file).read() 
     soup = Soup(handler) 
     for anytype in soup(lambda x: x.name=='anytype' and (hasattr(x, 'gains') and int(x.gains.string) > 0 or hasattr(x, 'losses') and int(x.losses.string) > 0)): 
       gain = anytype.gains.string 
       loss = anytype.losses.string 
       band = anytype.band.textualrepresentation.string 
       print gain, loss, band 

parseLog(sys.argv[1]) 

der Fehler Auch

Traceback (most recent call last): 
    File "./soup.py", line 15, in <module> 
    parseLog(sys.argv[1]) 
    File "./soup.py", line 9, in parseLog 
    for anytype in soup(lambda x: x.name=='anytype' and (hasattr(x, 'gains') and int(x.gains.string) > 0 or hasattr(x, 'losses') and int(x.losses.string) > 0)): 
    File "/Users/jacob/homebrew/lib/python2.7/site-packages/BeautifulSoup.py", line 659, in __call__ 
    return apply(self.findAll, args, kwargs) 
    File "/Users/jacob/homebrew/lib/python2.7/site-packages/BeautifulSoup.py", line 849, in findAll 
    return self._findAll(name, attrs, text, limit, generator, **kwargs) 
    File "/Users/jacob/homebrew/lib/python2.7/site-packages/BeautifulSoup.py", line 377, in _findAll 
    found = strainer.search(i) 
    File "/Users/jacob/homebrew/lib/python2.7/site-packages/BeautifulSoup.py", line 966, in search 
    found = self.searchTag(markup) 
    File "/Users/jacob/homebrew/lib/python2.7/site-packages/BeautifulSoup.py", line 924, in searchTag 
    or (markup and self._matches(markup, self.name)) \ 
    File "/Users/jacob/homebrew/lib/python2.7/site-packages/BeautifulSoup.py", line 983, in _matches 
    result = matchAgainst(markup) 
    File "./soup.py", line 9, in <lambda> 
    for anytype in soup(lambda x: x.name=='anytype' and (hasattr(x, 'gains') and int(x.gains.string) > 0 or hasattr(x, 'losses') and int(x.losses.string) > 0)): 
AttributeError: 'NoneType' object has no attribute 'string' 

zurückkehrt, wenn ich die for-Schleife zu

for anytype in soup(lambda x: x.name=='anytype' and (hasattr(x, 'gains'))): 
     gain = anytype.gains.string 
     print gain 

reduziere ich noch

bekommen
Traceback (most recent call last): 
    File "./soup.py", line 13, in <module> 
    parseLog(sys.argv[1]) 
    File "./soup.py", line 10, in parseLog 
    gain = anytype.gains.string 
AttributeError: 'NoneType' object has no attribute 'string' 

Antwort

1

sollte der Code sein:

for anytype in soup(lambda x: x.name=='anytype' and (int(x.gains.string) > 0 or int(x.losses.string) > 0)): 
    gain = anytype.gains.string 
    loss = anytype.losses.string 
    band = anytype.band.textualrepresentation.string 
    print gain loss band 

Python ||or ist, und wir brauchen Zeichenfolge Zahl zu konvertieren, bevor ganzzahligen Vergleich durchzuführen, z.B. int(x.gains.string). Ich hoffe das hilft.

+1

tatsächlich Sie könnten als eine Zeichenfolge verlassen und machen 'x.gains.string> '0'' – maxymoo

+0

sinnvoll, aber nur für den Fall, dass Daten in guter Form sind. Oder es wird sehr schwierig sein, einen Fehler zu finden, wenn die Zeichenfolge '' '>' 0 '' oder '' foo '' '0' 'ist. Wir brauchen einen Fehler, anstatt ein falsches Ergebnis zu erzeugen. – digitake

+0

Ich bekomme immer noch 'Datei './soup.py ", Zeile 13 Druckverstärkung Verlustband ^ SyntaxError: ungültige Syntax" als ein Fehler mit diesem Code – Jacob

2

Ich würde das gesamte Dokument in einen Pandas Datenrahmen analysieren, dann tun, was auch immer Manipulationen nach; Dadurch kann der Datenbereinigungsprozess transparenter und einfacher zu verstehen sein.

Ich werde xmltojson hier verwenden, da ich nicht vertraut mit schöner Suppe bin (obwohl ich in „Dokument“ Tags, um die ganze Sache zu umschließen hatte, da es zu machen, um gültige XML):

import xmltojson 
import pandas as pd 

with open(file) as f: 
    j = eval(xmltojson.parse("<document> "+ f.read() + "</document>")) 

df = pd.io.json.json_normalize(j['document']['anytype']) 
df.columns = ['type', 'band', 'gain', 'loss', 'struct'] 
df[(df.gain > '0') | (df.loss > '0')][['band', 'gain', 'loss']] 

     band gain loss 
0 22q11.1 2 1 
1 22q11.2 0 1 
+0

Ich erhalte diesen Fehler, wenn ich diesen Code verwende 'Traceback (zuletzt letzter Aufruf): Datei" ./script .py ", Zeile 5, in mit open (Datei) als f: TypeError: erzwingt Unicode: brauche String oder Puffer, type found' – Jacob

Verwandte Themen