我有一个包含不同长度行的.txt
文件,每一行都是表示一条轨迹的系列点。由于每个轨迹的长度不同,因此行的长度也不同。也就是说,每一行的列数都不相同。
据我所知,Python中的genfromtxt()
模块需要每一行的列数相同。
>>> import numpy as np
>>>
>>> data=np.genfromtxt('deer_1995.txt', skip_header=2)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python27\lib\site-packages\numpy\lib\npyio.py", line 1638, in genfromtxt
raise ValueError(errmsg)
ValueError: Some errors were detected !
Line #4 (got 2352 columns instead of 1824)
Line #5 (got 2182 columns instead of 1824)
Line #6 (got 1412 columns instead of 1824)
Line #7 (got 1650 columns instead of 1824)
Line #8 (got 1688 columns instead of 1824)
Line #9 (got 1500 columns instead of 1824)
Line #10 (got 1208 columns instead of 1824)
此外,它还能通过 filling_values
的帮助填充缺失值。但我认为这会引发不必要的麻烦,我希望避免。
那么有没有最好(Pythonic)的方法可以在不填充“缺失值”的情况下简单导入数据集?