How to control genfromtxt to read rows specified?

穿精又带淫゛_ 提交于 2019-12-10 23:24:18

问题


genfromtxt can skip header and footer lines and speicfy which columns to use. But how can I control how many lines to read?

Sometimes a txt file might contain several blocks with different shape. For example,

a=StringIO('''
1,2,3
1,2,3
2,3
2,3
''')
genfromtxt(a,delimiter=',',skip_header=1)

This will raise an error,

ValueError: Some errors were detected !
    Line #4 (got 2 columns instead of 3)
    Line #5 (got 2 columns instead of 3)

Of couse, I can do it like this:

a=StringIO('''
1,2,3
1,2,3
2,3
2,3
''')
genfromtxt(a,delimiter=',',skip_header=1,skip_footer=2)

It's ugly as I have to calculate the number of rows under the block.

However I wish something like

genfromtxt(a,delimiter=',',skip_header=1,nrows=2)

that would be more clear.

Anyone have a good idea about that? Or use other function?


Update 2015 Oct

This question has been solved in new version of Numpy.

genfromtxt now have a new keywords named max_rows which allow one to control the number of lines to read, cf here.


回答1:


You can use the invalid_raise = False to skip reading the lines that are missing some data. E.g.

b = np.genfromtxt(a, delimiter=',', invalid_raise=False)

This will give you a warning, but will not raise an exception.



来源:https://stackoverflow.com/questions/25925348/how-to-control-genfromtxt-to-read-rows-specified

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!