行と列の数値のみを含むテキストファイルを開いて、新しいファイルに保存するプログラムを作成しようとしています。 私は列を選択する部分は、行の一部は動作しません。 条件x> 10e13の行を選択する必要があります(xは特定の列の値です)。 これは私が書いたコードです:条件付き行を選択してファイルに保存する方法
import numpy as np
matrix = np.loadtxt('file.dat')
#select columns:
column_indecies = [0]
selected_columns = matrix[:,column_indecies]
x=10E13
#select lines:
for line in matrix:
if float(line) > x:
selected_lines = line
selected_matrix = matrix[selected_lines,selected_columns]
# output:
np.savetxt('new_file.dat', selected_matrix, fmt='%1.4f')
そしてこれは、端末上の出力エラーです:
selected_matrix = matrix[selected_lines]
IndexError: only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer or boolean arrays are valid indices
私は初心者だけど、誰も私を助けることができますか?私はMacユーザーで、Python 2.7を使用しています。 これは私の入力データの小さなサンプルです:
185100000000000.0000
121300000000000.0000
257800000000000.0000
43980000000000.0000
'フロート(ライン)> X場合:'これはエラーの原因であり、またselected_lines [] =行[] 'ライン –
'のタブスペースを追加私は知っていますが、どうすれば修正できますか? @NarenMurali –
@AlessandroPeca 'x'の後にコロンを追加し、次の行を4つの空白でインデントします。 –