運行下面的代碼在一個文本文件中給定數量的列對值進行比較,在這種情況下,440如果在for循環不工作,即使條件滿足
with open('test.txt', 'a+') as input:
for line in input:
columns = line.split(" ")
print columns[5] #test
if columns[5] == '440':
print 'match'
test.txt的是隻是:
0 0 0 0 0 1
0 0 0 0 0 440
0 0 0 0 0 1
0 0 0 0 0 440
0 0 0 0 0 1
0 0 0 0 0 1
打印列[5]位打印出從txt文件正確的價值,但即使它匹配440,如果for循環不工作中
謝謝你的任何幫助
請使用'print repr(columns [5])'來代替並顯示我們那個的輸出。 –
你真的打算以''a +''模式打開文件嗎?這是* append *模式。 –
如果你的文本文件確實包含數字,那麼你爲什麼不比較它像: - 'int(columns [0])== 440'? –