mustang_39211 mustang_39211 - 1 year ago 71
Python Question

Using greater than expression to filter text file lines?

I have a text file with multiple lines and want to find which lines have values greater than 85%.

'workdata worka worka1 size 84% total'
'workdata workb workb1 size 89% total'
'workdata workc workc1 size 63% total'
'workdata workd workd1 size 94% total'

Can someone please show how I can get just the sentences with 85% or more in the fifth column?

Answer Source

If you know the percentage will always be in the 5th column, then just split each row on space, remove the percentage sign, and turn it into a float. Something like this:

lines = open("fileName", "r").read().splitlines()
for row in lines:
    if float(row.split()[4].replace("%",""))>85:
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download