シミュレーションコードの出力ファイル(300008行)が大きく表示されません。この関数を使用してファイルの2つの列を読み取っていますファイルからspcific列を読み取る
def read2(filename,i1,i2):
"Read 2 ARRAYS from the i1-th and i2-th columns of a text file"
from numpy import * # import numpy package
f = open(filename,'r') # open the file
n = len(f.readlines()) # get number of rows
L1= [] ; L2=[] # create empty lists
f.seek(0) # go to beginning of file
for k in range(n):
s = f.readline() # read current row in string format
e = s.split() # split the elements of the string between white spaces
if "#" not in e[0]:
L1.append(e[i1-1])
L2.append(e[i2-1])
f.close() # close input file
A1 = convert(L1,i1)
A2 = convert(L2,i2)
return A1,A2 # return arrays A1 & A2 to __main__ .
問題は次のとおりです。それは常に私にIndexErrorを与えます:リストのインデックスを範囲外にリストします。
PS。 convertは、文字列リストを配列に変換する別の関数です。
ここで、エラーは正確に起こっていますか?コードのどの行に – ThePredator
多くの場合、行数を取得するためにファイルを2回読み込んでいます。準最適! –
'np.genfromtxt'では、使用する列を指定できます。 – hpaulj