解析文本文件并提取特定列 [英] Parse a text file and extract a specific column

查看:72
本文介绍了解析文本文件并提取特定列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从文本文件中读取值,直到那是一个浮点值.可以说我有以下文件:

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.

预先感谢您的帮助

推荐答案

创建状态机readline()/for line in file并根据与不同行格式相对应的正则表达式执行re.match()结果并执行操作和/或切换状态.

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.

这篇关于解析文本文件并提取特定列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆