Python 解析此文件以访问末尾以制表符分隔的表的最佳方法是什么?
我有大约15000个文本文件,其格式示例如下: 我感兴趣的部分是末尾的表格,它看起来像:Python 解析此文件以访问末尾以制表符分隔的表的最佳方法是什么?,python,pandas,parsing,Python,Pandas,Parsing,我有大约15000个文本文件,其格式示例如下: 我感兴趣的部分是末尾的表格,它看起来像: 1 1 GLY HA2 H 3.55 . 2 2 1 GLY HA3 H 3.76 . 2 3 2 VAL H H 8.52 . 1 4 2 VAL HA H 4.20
1 1 GLY HA2 H 3.55 . 2
2 1 GLY HA3 H 3.76 . 2
3 2 VAL H H 8.52 . 1
4 2 VAL HA H 4.20 . 1
5 2 VAL HB H 2.02 . 1
我没有很多解析文件的经验,但我想这里的很多人都会。我可以得到一些关于如何以编程方式提取文件的这一部分的建议吗
例如,是否有一种仅在两行之间读取文件的方法:
_Chem_shift_ambiguity_code
及
最好的方法是使用正则表达式使用readline方法搜索每一行,直到找到适当的部分,然后切换“开启”某个连续将行附加到数据帧的内容吗
提前谢谢你 这种解析的一种简单方法是使用布尔变量记录我们是否在要处理的块内,并在找到关键字时切换它:
with open(filename) as fd:
inblock = False
for line in fd:
if inblock: # we are inside the block here
if len(line.strip()) == 0:
continue # ignore blank lines inside block
elif 'stop_' in line:
inblock = False
break # stop processing the file
else: # ok, we can process that line
...
else: # still waiting for the initial keyword
if '_Chem_shift_ambiguity_code':
inblock = True # ok we have found the beginning of the block
你到底试过什么?你能提供一个你目前正在使用的例子吗?你对这个例子有什么问题吗?目前很难确切知道您在寻求什么帮助。例如,我可以将文件的每一行读入内存,但我不知道如何开始搜索表中以逻辑开头的适当行。您好,是的,谢谢-我已经构建了类似的东西,但是在搜索keywords@Vranvs这就是为什么规则要求您提供代码的原因。如果你有,有人可能会解释错误并说如何修复…对不起-我在发布这个问题后写了它,试图同时解决它。我想我最想肯定的是这是正确的方法。非常感谢你!
with open(filename) as fd:
inblock = False
for line in fd:
if inblock: # we are inside the block here
if len(line.strip()) == 0:
continue # ignore blank lines inside block
elif 'stop_' in line:
inblock = False
break # stop processing the file
else: # ok, we can process that line
...
else: # still waiting for the initial keyword
if '_Chem_shift_ambiguity_code':
inblock = True # ok we have found the beginning of the block