ファイルが最後を除いて唯一の2
In [182]: fname='../Downloads/pastie-10860707.txt'
In [183]: np.fromregex(fname,regexp=pattern,dtype=float)
...
np.fromregex(fname,regexp=pattern,dtype=float)
/usr/lib/python3/dist-packages/numpy/lib/npyio.py in fromregex(file, regexp, dtype)
1240 # Create the new array as a single data-type and then
1241 # re-interpret as a single-field structured array.
-> 1242 newdtype = np.dtype(dtype[dtype.names[0]])
1243 output = np.array(seq, dtype=newdtype)
1244 output.dtype = dtype
TypeError: 'NoneType' object is not subscriptable
は、単純な 'BR' を搭載している、コンマ分離、3つの数字のperlineで、複数の行を持つファイルは次のようになり、読み:
In [184]: txt
Out[184]: b'2.75386225e+00,1.80508078e+00,2.95729122e+00,\n-4.21413726e+00, -3.38139076e+00, -4.22751379e+00,\n ... 4.23010784e-01, -1.14839331e+00, -9.56098910e-01,\n -1.15019836e+00, 1.13845303e-06'
最後の行に番号がないと、genfromtxt
の問題が発生します。
パターンの選択が間違っています。デリミタパターンのように見えます。あなたがに変換することができタプルのリストを作成したパターンを考え出す必要があるfromregex
を使用したい場合は
regexp = r"(\\d+)\\s+(...)"
fromregex
が
seq = regexp.findall(file.read()) # read whole file and group it
output = np.array(seq, dtype=dtype) # make array from seq
を行います。しかし、fromregex
ドキュメント内のパターンは、グループを作成します直接配列。私は、当面の問題はdtype
であることがわかり、エラーmesssageで再び見
================
けれども。 dtype=float
は、この関数の有効なdtype仕様ではありません。複合dtype(構造化)が必要です。
エラーがfloat
があなたのdtype
パラメータであり、この作用により生成されます
In [189]: np.dtype(float).names[0]
...
TypeError: 'NoneType' object is not subscriptable
しかし、パターンが
In [194]: pattern.findall(txt)
Out[194]:
[b',',
b',',
b',',
b'\n',
b',',
b' ',
b' ',
....]
ないというタプルのリストを作成したので、これをやろうとしていますそれは期待された。
==================
私は一時的に問題を回避するためにusecols
を使用してい
In [213]: np.genfromtxt(txt.splitlines(),delimiter=',',usecols=[0,1])
Out[213]:
array([[ 2.75386225e+00, 1.80508078e+00],
[ -4.21413726e+00, -3.38139076e+00],
[ 7.46991792e-01, -1.08010066e+00],
...
[ 4.23010784e-01, -1.14839331e+00],
[ -1.15019836e+00, 1.13845303e-06]])
を使用してファイルを読み込むことができます最後の行には2つの数字しかありません。
\n
を削除し、カンマで分割すると、結果のテキストフィールドを直接np.array
で解析できます。
In [266]: pattern=re.compile(br"(\d+\.\d+e[\+\-]\d+)")
In [267]: np.fromregex(fname,regexp=pattern,dtype=np.dtype([('f0',float)]))['f0']
Out[267]:
array([ 2.75386225e+00, 1.80508078e+00, 2.95729122e+00,
4.21413726e+00, 3.38139076e+00, 4.22751379e+00,
...
4.23010784e-01, 1.14839331e+00, 9.56098910e-01,
1.15019836e+00, 1.13845303e-06])
今、私は構造化された配列を作成し、そのフィールドを抽出てる場合:
In [231]: txt1=txt.replace(b'\n',b'').split(b',')
In [232]: np.array(txt1,float)
Out[232]:
array([ 2.75386225e+00, 1.80508078e+00, 2.95729122e+00,
-4.21413726e+00, -3.38139076e+00, -4.22751379e+00,
...
4.23010784e-01, -1.14839331e+00, -9.56098910e-01,
-1.15019836e+00, 1.13845303e-06])
このパターンは、小数点と科学的表記を含んでいます。その周りに道があるかもしれません。しかし、fromregex
は、構造化されたdtypesの使用を好むようです。
完全なトレースバックを投稿してください。 – kindall
投稿したコードに 'np.genfromtxt'を使用していません。 – user2357112
外部のウェブサイトにデータを投稿しないでください。それをあなたの質問にコピーしてください。 – MattDMo