2

使用numpyin python3.3,可以genfromtxt用来导入非方表吗?我想从一个看起来像这样的文件中读取:

标题

2 1

-0.6634 -0.3830 -0.0000℃

0.6634 0.3830 -0.0000 转

1 2 1 1

每行以 . 结尾\n。显然这张桌子不是方形的。我没有成功使用loadtxtor genfromtxt。他们都想要方桌。以下是示例及其结果:

>>> with open('propane.ct','r') as f:
       txt = f.read()
       data = np.genfromtxt(io.BytesIO(txt.encode()), delimiter='\n', dtype=None)

[b'propane.ct' b'3 2' b'-1.3268   ' b'0.0000    ' b'1.3268   -' b'1 2  1  1'
 b'2 3  1  1']

>>> np.loadtxt('propane.ct',skiprows=1)
array([[ 3.    ,  2.    ],
       [-1.3268, -0.383 ],
       [ 0.    ,  0.383 ],
       [ 1.3268, -0.383 ],
       [ 1.    ,  2.    ],
       [ 2.    ,  3.    ]])

或者

>>> np.genfromtxt('propane.ct')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/opt/local/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/site-packages/numpy/lib/npyio.py", line 1638, in genfromtxt
    raise ValueError(errmsg)
ValueError: Some errors were detected !
    Line #2 (got 2 columns instead of 1)
    Line #3 (got 4 columns instead of 1)
    Line #4 (got 4 columns instead of 1)
    Line #5 (got 4 columns instead of 1)
    Line #6 (got 4 columns instead of 1)
    Line #7 (got 4 columns instead of 1)

这些文件是由专有软件生成的,所以我无法在初始文件创建期间对表进行平方。我曾考虑为每一行添加列以使其成为正方形,但这可能比使用numpy内置插件效率低。

我做错了什么还是我必须编写自己的解析器?

4

1 回答 1

2

您可以使用np.genfromtxt设置参数skip_header,例如:

np.genfromtxt('my_file.csv', skip_header=4, dtype=str)
#array([['-0.6634', '-0.3830', '-0.0000', 'C'],
#       ['0.6634', '0.3830', '-0.0000', 'R'],
#       ['1', '2', '1', '1']],
#      dtype='|S7')
于 2013-10-10T21:11:51.297 回答