在python3.3中使用numpy,能否使用genfromtxt导入非方表?我想从一个看起来像这样的文件中读取:
title
2 1
-0.6634 -0.3830 -0.0000 C
0.6634 0.3830 -0.0000 R
1 2 1 1
每行以 \n 结尾。显然这张 table 不是正方形的。我没有成功使用 loadtxt 或 genfromtxt。他们都想要方 table 。以下是示例及其结果:
>>> 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 内置函数效率低。
是我做错了什么还是我必须编写自己的解析器?
请您参考如下方法:
您可以使用 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')
