1

savetxt で保存するときに numpy-array をフォーマットできません。

from numpy import *

a = loadtxt('frequenz.txt')
b = sum(a[0:10,1:4], 1)/3-20741
savetxt('solution.txt', b, fmt='%.2f', delimiter='\t', header='average')

エラーが表示されます:

fh.write(asbytes(format % tuple(row) + newline))
UnboundLocalError: local variable 'format' referenced before assignment

fmt を使用せずに値を保存すると機能しますが、小数点以下 2 桁のみが必要です。

from numpy import *

a = loadtxt('frequenz.txt')
b = sum(a[0:10,1:4], 1)/3-20741
savetxt('solution.txt', b, delimiter='\t', header='average')

ソリューション.txt:

# average
3.666666666667879326e+00
6.666666666667879326e+00
9.333333333332120674e+00
1.300000000000000000e+01
1.533333333333212067e+01
1.833333333333212067e+01
2.133333333333212067e+01
2.466666666666787933e+01
2.766666666666787933e+01
3.066666666666787933e+01

頻度.txt:

#0  20741   20741   20741
6   20745   20745   20744   20739   20739   20738
12  20748   20747   20748   20736   20736   20736
18  20750   20751   20750   20732   20733   20732
24  20754   20754   20754   20730   20730   20730
30  20756   20756   20757   20727   20727   20726   
36  20760   20759   20759   20723   20723   20724
42  20762   20762   20763   20721   20721   20720
48  20766   20766   20765   20718   20718   20718
54  20768   20769   20769   20715   20715   20714
60  20771   20772   20772   20712   20712   20712   

ご協力ありがとうございました!マーティン

4

1 に答える 1

1

私の解決策: 私は常にエディター BBEdit に Python コードを入力し、その後ターミナルで実行します。これは機能せず、エラーが発生します。ターミナルに直接入力すると正常に動作します。Pythonドキュメントの私のデフォルトの頭は

from __future__ import (print_function, division, unicode_literals, absolute_import)

エラーの原因は

unicode_literals

多分それは他の人を助けるでしょう。よろしく。

于 2013-10-20T18:49:54.097 に答える