Python 3.5で具体的にフォーマットされたファイル(Butcher tableau)を読み込もうとしています。 ファイルこの(タブ区切り)のようになります。例えばファイルからButcherテーブルを読み取るためのエレガントで読みやすい方法
S
a1 b11 b12 ... b1S
a2 b21 b22 ... b2S
...
aS bS1 bS2 ... bSS
0.0 c1 c2 ... cS
[tolerance]
、(タブ区切り)
2
0.0 0.0 0.0
1.0 0.5 0.5
0.0 0.5 0.5
0.0001
私はCで書いているようなので、私のコードは見た目よりニシキヘビのアプローチがありますこのファイルを解析するには?おそらく、ここで使用できるnumpyメソッドがありますか?
#the data from .dat file
S = 0 #method order, first char in .dat file
a = [] #S-dim left column of buther tableau
b = [] #S-dim matrix
c = [] #S-dim lower row
tolerance = 0 # for implicit methods
def parse_method(file_name):
'read the file_name, process lines, produce a Method object'
try:
with open('methods\\' + file_name) as file:
global S
S = int(next(file))
temp = []
for line in file:
temp.append([float(x) for x in line.replace('\n', '').split('\t')])
for i in range(S):
a.append(temp[i].pop(0))
b.append(temp[i])
global c
c = temp[S][1:]
global tolerance
tolerance = temp[-1][0] if len(temp)>S+1 else 0
except OSError as ioerror:
print('File Error: ' + str(ioerror))
実際のファイルを実際の値で記述することはできますか?理解しやすくなります。 –
あなたが正しいです、私は例を追加しました –
Ok。私は答えを追加します。 –