Amjedアッバス:
私は、Python言語で新たなんです。私は列を分割し、以下のような多くの行の制限値を超えるためにカウントするために、テキストファイルをロードしようとしました。
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
ジオフォン異常な仕様10 =抵抗(オーム)、11 =ノイズ(μV)、12 =リーク(MO)、13 =傾き(%)、地震探査探査のためのQCとして私の仕事のため、これらの値
私のコードは次のようなものです:
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])
そして私は、このエラーに会いました:
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
誰の助けを私は喜ばせることはできますか?
SK:
純粋なPythonの提案
あなたのためのループは文字で反復され、私はそれはあなたが望むものではないのですね。
あなたは代わりに、これを試すことができます。
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
ここでは、しきい値に対する数値を確認してください。
あなたの代わりに文字列の長さをチェックしたい場合は、自分でコードを変更することができ、それは良い練習になりますが、あなたのデータサンプルでは、このようなケースを持っていません。