3

numpy genfromtxt でばかばかしいほど単純な問題のように見えるものがあります。これは私のコードの(非常に)単純化されたバージョンです:

import numpy as np

in_file_1 = raw_input ('enter name of template file to be scaled:\n')
spec_1 = np.genfromtxt(in_file_1, delimiter = [8,24], dtype =float)
print spec_1

私が読んでいるファイルは完全に単純で、2 つの列にヘッダーなどはありません。

6392.01   0.90286163
6392.05   0.88731778
6392.09   0.87789831
6392.13   0.87716535
6392.16   0.88523003
6392.20   0.90948176
6392.24   0.93056874
6392.28   0.95782283
6392.32   0.98056805
6392.36   0.99623797
6392.39   0.99458828
6392.43   0.9848269
6392.47   0.96011146
6392.51   0.92864767

Pythonコマンドラインでgenfromtxtを使用して上記を読むと、期待どおりに2列の配列が得られます。

>>> import numpy as np
>>> in_file_1 = raw_input ('enter name of template file to be scaled:\n')
enter name of template file to be scaledl_1714650_052_no_head.txt
>>> spec_1 = np.genfromtxt(in_file_1, delimiter = [8,24], dtype =float)
>>> spec_1
array([[  6.39201000e+03,   9.02861630e-01],
       [  6.39205000e+03,   8.87317780e-01],
       [  6.39209000e+03,   8.77898310e-01],
       [  6.39213000e+03,   8.77165350e-01],
       [  6.39216000e+03,   8.85230030e-01],
       [  6.39220000e+03,   9.09481760e-01],
       [  6.39224000e+03,   9.30568740e-01],
       [  6.39228000e+03,   9.57822830e-01],
       [  6.39232000e+03,   9.80568050e-01],
       [  6.39236000e+03,   9.96237970e-01],
       [  6.39239000e+03,   9.94588280e-01],
       [  6.39243000e+03,   9.84826900e-01],
       [  6.39247000e+03,   9.60111460e-01],
       [  6.39251000e+03,   9.28647670e-01]])
>>> 

しかし、この投稿の上部にあるスクリプト (read_test.py) として実行すると、文字列の単一の列が返されます。

[scrooge:Acc_cont_removal/All_stars/Test] darryl% python read_test.py 
enter name of template file to be scaled:
l_1714650_052_no_head.txt
[[  6.39201000e+03   9.02861630e-01]
 [  6.39205000e+03   8.87317780e-01]
 [  6.39209000e+03   8.77898310e-01]
 [  6.39213000e+03   8.77165350e-01]
 [  6.39216000e+03   8.85230030e-01]
 [  6.39220000e+03   9.09481760e-01]
 [  6.39224000e+03   9.30568740e-01]
 [  6.39228000e+03   9.57822830e-01]
 [  6.39232000e+03   9.80568050e-01]
 [  6.39236000e+03   9.96237970e-01]
 [  6.39239000e+03   9.94588280e-01]
 [  6.39243000e+03   9.84826900e-01]
 [  6.39247000e+03   9.60111460e-01]
 [  6.39251000e+03   9.28647670e-01]]
[scrooge:Acc_cont_removal/All_stars/Test] darryl% 

区切り文字などのさまざまな構成を試しましたが、何が起こっているのかわかりません。このコードは数日前に正常に機能しました。Python v2.7 を使用して OSX (Lion) で実行しています。

どんなアイデアもありがたく受け取った。ダリル

4

1 に答える 1

4

あなたはまさにあなたが得るべき答えを得ています!

>>> numpy.genfromtxt('testfile', delimiter=[8,24], dtype = float)
array([[  6.39201000e+03,   9.02861630e-01],
       [  6.39205000e+03,   8.87317780e-01],
       [  6.39209000e+03,   8.77898310e-01],
       [  6.39213000e+03,   8.77165350e-01],
       [  6.39216000e+03,   8.85230030e-01],
       [  6.39220000e+03,   9.09481760e-01],
       [  6.39224000e+03,   9.30568740e-01],
       [  6.39228000e+03,   9.57822830e-01],
       [  6.39232000e+03,   9.80568050e-01],
       [  6.39236000e+03,   9.96237970e-01],
       [  6.39239000e+03,   9.94588280e-01],
       [  6.39243000e+03,   9.84826900e-01],
       [  6.39247000e+03,   9.60111460e-01],
       [  6.39251000e+03,   9.28647670e-01]])
>>> print _
[[  6.39201000e+03   9.02861630e-01]
 [  6.39205000e+03   8.87317780e-01]
 [  6.39209000e+03   8.77898310e-01]
 [  6.39213000e+03   8.77165350e-01]
 [  6.39216000e+03   8.85230030e-01]
 [  6.39220000e+03   9.09481760e-01]
 [  6.39224000e+03   9.30568740e-01]
 [  6.39228000e+03   9.57822830e-01]
 [  6.39232000e+03   9.80568050e-01]
 [  6.39236000e+03   9.96237970e-01]
 [  6.39239000e+03   9.94588280e-01]
 [  6.39243000e+03   9.84826900e-01]
 [  6.39247000e+03   9.60111460e-01]
 [  6.39251000e+03   9.28647670e-01]]

印刷と結果の表現の取得の違いだけです。

>>> result = numpy.genfromtxt('testfile', delimiter=[8,24], dtype = float)
>>> repr(result)
'array([[  6.39201000e+03,   9.02861630e-01],\n       [  6.39205000e+03,   8.87317780e-01],\n       [  6.39209000e+03,   8.77898310e-01],\n       [  6.39213000e+03,   8.77165350e-01],\n       [  6.39216000e+03,
  8.85230030e-01],\n       [  6.39220000e+03,   9.09481760e-01],\n       [  6.39224000e+03,   9.30568740e-01],\n       [  6.39228000e+03,   9.57822830e-01],\n       [  6.39232000e+03,   9.80568050e-01],\n
[  6.39236000e+03,   9.96237970e-01],\n       [  6.39239000e+03,   9.94588280e-01],\n       [  6.39243000e+03,   9.84826900e-01],\n       [  6.39247000e+03,   9.60111460e-01],\n       [  6.39251000e+03,   9.2864
7670e-01]])'

>>> str(result)
'[[  6.39201000e+03   9.02861630e-01]\n [  6.39205000e+03   8.87317780e-01]\n [  6.39209000e+03   8.77898310e-01]\n [  6.39213000e+03   8.77165350e-01]\n [  6.39216000e+03   8.85230030e-01]\n [  6.39220000e+03
 9.09481760e-01]\n [  6.39224000e+03   9.30568740e-01]\n [  6.39228000e+03   9.57822830e-01]\n [  6.39232000e+03   9.80568050e-01]\n [  6.39236000e+03   9.96237970e-01]\n [  6.39239000e+03   9.94588280e-01]\n [
 6.39243000e+03   9.84826900e-01]\n [  6.39247000e+03   9.60111460e-01]\n [  6.39251000e+03   9.28647670e-01]]'

str バージョンにはコンマがなく、repr バージョンにはあることに注意してください。

于 2012-05-01T11:55:58.523 に答える