Parse a text file and extract a specific column

2019-09-06 18:23发布

I would like to read values from a text file until that is a float value. Lets say I have the following file:

Time  
Detailes
Distance   Fz    Fx.
0.0000   249       4      
0.0002   247      33     
0.0004   246      49     
0.0006   243      61     

Distance   Fz    Fx.
0.0000   249       4      
0.0002   247      43    
0.0004   246      59     
0.0006   243      70

And I need only the values under the first Distance column.

So something like skip the first few rows, then read values from the first column while it is float.

Thanks your help in advance

标签: python file text
1条回答
仙女界的扛把子
2楼-- · 2019-09-06 18:31

Make a state machine that will readline()/for line in file and do things and/or switch state depending on re.match() result on regexes corresponding to different lines' formats.

This is the standard approach to parse input that isn't so complex as to require a lexical analyzer.

查看更多
登录 后发表回答