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
bekommenTraceback (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'
tatsächlich Sie könnten als eine Zeichenfolge verlassen und machen 'x.gains.string> '0'' – maxymoo
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
Ich bekomme immer noch 'Datei './soup.py ", Zeile 13 Druckverstärkung Verlustband ^ SyntaxError: ungültige Syntax" als ein Fehler mit diesem Code – Jacob