How to split column and make count for over limit values

Amjed Abbas :

I'm new in python language. I tried to load a text file, to split columns and count for over limit values for many rows such as the following:

Box Type    Serial Nb   Sensor Type Line Name   Point Nb    Point Index Segd Code   Set Grid Easting    Set Grid Northing   Surface Elevation   Resistance(ohm) Noise (µV)  Leakage(Mo) Tilt (%)    Latest Update   
FDU-428 12263085    1   4619    1169    1   2   566443.8    3456742.2   8.0 132.23  5.78    5.0 -1.33   Sat Dec 15 12:52:17 AST 2018    
FDU-428 5848688 1   4589    1170    1   2   565641.6    3455415.0   7.4 133.2   4.99    5.0 -1.29   Sat Dec 15 12:52:17 AST 2018    
FDU-428 12318634    1   4619    1168    1   2   566401.8    3456769.2   7.5 132.3   6.26    5.0 -0.33   Sat Dec 15 12:52:17 AST 2018    
FDU-428 12280956    1   4589    1164    1   2   565390.0    3455578.5   7.4 133.46  7.85    5.0 -0.96   Sat Dec 15 12:52:17 AST 2018    
FDU-428 11271012    1   4607    1180    1   2   566551.1    3455897.5   7.1 132.8   5.81    5.0 -0.36   Sat Dec 15 12:52:17 AST 2018    
FDU-428 12245682    1   4661    1337    2   2   574607.9    3453890.8   6.7 133.32  4.14    5.0 -1.19   Sat Dec 15 12:52:17 AST 2018    

these value for geophone abnormal specification 10 =Resistance(ohm),11=Noise (µV),12=Leakage(Mo) ,13=Tilt (%) , i work as qc for seismic survey exploration

My code is like the following:

myfile = open('aaa.txt','r')
myvar=(myfile.read())

rows = (myvar.split('\n'))
for i in range(1,len(rows)):
    if float(rows[i].split('   ')[10]) > 140:
        print (rows[i].split('   ')[10])

and I met this error:

Traceback (most recent call last):
     File "D:/Python/python/import_text2.py", line 11, in <module>
    if float(rows[i].split('   ')[10]) > 140:
IndexError: list index out of range 

Can anyone help me please?

s.k :

A pure Python proposal

Your for loop is iterating of letters, I guess it's not what you want.

You can try this instead:

text_file = "aaa.txt"

with open(text_file,'r') as f:
    data = f.read()

threshold = 140

for row in data.split('\n'):
    # by default, the .split() method use the 
    # white space (any amount) as the separator.
    for word in row.split():
        try:
            if int(word) > threshold:
                print("{} is greater than {}".format(word, threshold))
        except:
            print("Cannot convert word to number.")
            pass


Here, we check the numerical value against the threshold.
If you want to check for the string length instead, you can modify the code on your own, it will be a good exercise, but in your data sample you don't have such cases.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=405511&siteId=1