简体   繁体   中英

Get line which contains string

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 input is just a string, then for line in input doesn't iterate lines, it iterates characters. So 'lucas' would never be found in a one-character string and tableInput would 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:

for line in input.split('\n'):
    ...

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:

how I would get the table entry for the row that contains the string 'lucas' any hints ?

Use td.parent to get the containing row, td.parent.parent to get the containing table/tbody, and so on.

If you wanted to get the V or X in the next column, you could say something like:

tr= soup.find(text= re.compile('lucas')).parent.parent
vorx= tr.findAll('td')[1].find('span').string

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM