使用numpy
in python3.3
,可以genfromtxt
用来导入非方表吗?我想从一个看起来像这样的文件中读取:
标题
2 1
-0.6634 -0.3830 -0.0000℃
0.6634 0.3830 -0.0000 转
1 2 1 1
每行以 . 结尾\n
。显然这张桌子不是方形的。我没有成功使用loadtxt
or 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
内置插件效率低。
我做错了什么还是我必须编写自己的解析器?