I’m trying to get a line from a textfile that contains a certain sequence of characters :
my input :
<tr><td>lucas.vlan77.be</td> <td><span style="color:green;font-weight:bold">V</span></td> <td><span style="color:green;font-weight:bold">V</span></td> <td><span style="color:green;font-weight:bold">V</span></td> </tr>
<tr><td>jeanpierre.vlan77.be</td> <td><span style="color:green;font-weight:bold">V</span></td> <td><span title="Cannot connect to 193.191.187.25:22345." style="color:red;font-weight:bold">X</span></td> <td><span title="No response from DNS at 193.191.187.25." style="color:red;font-weight:bold">X</span></td> </tr>
<tr><td>sofie.vlan77.be</td> <td><span style="color:green;font-weight:bold">V</span></td> <td><span title="Cannot connect to 193.191.187.26:22345." style="color:red;font-weight:bold">X</span></td> <td><span title="No response from DNS at 193.191.187.26." style="color:red;font-weight:bold">X</span></td> </tr>
<tr><td>thomas.vlan77.be</td> <td><span style="color:green;font-weight:bold">V</span></td> <td><span style="color:green;font-weight:bold">V</span></td> <td><span style="color:green;font-weight:bold">V</span></td> </tr>
Now I need to get the line that contains lucas, I tried this with beautifulsoup, but it is not meant to get a line only content of html tags, so I tried with a regular in operator :
def soupParserToTable(self,input):
global header
soup = self.BeautifulSoup(input)
header = soup.first('tr')
tableInput='0'
for line in input:
if 'lucas' in line:
tableInput = line
print tableInput
However it keeps returning 0 instead of
<tr><td>lucas.vlan77.be</td> <td><span style="color:green;font-weight:bold">V</span></td> <td><span style="color:green;font-weight:bold">V</span></td> <td><span style="color:green;font-weight:bold">V</span></td> </tr>
If
inputis just a string, thenfor line in inputdoesn’t iterate lines, it iterates characters. So'lucas'would never be found in a one-character string andtableInputwould not be assigned. The line-based iteration behaviour only happens when the object is a file.If you wanted to loop through each line of a string you’d have to do:
Since you have BeautifulSoup available I’d say it would be much better to use that to read the value from the first cell in each row, rather than rely on crude and fragile string-searching.
ETA:
Use
td.parentto get the containing row,td.parent.parentto get the containing table/tbody, and so on.If you wanted to get the
VorXin the next column, you could say something like: