python - 从和读取特定文本行的文件 [英] python - Read file from and to specific lines of text

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

问题描述

我不是在谈论特定的行号,因为我正在读取具有相同格式但长度不同的多个文件.
假设我有这个文本文件:

I'm not talking about specific line numbers because i'm reading multiple files with the same format but vary in length.
Say i have this text file:

Something here...  
... ... ...   
Start                      #I want this block of text 
a b c d e f g  
h i j k l m n  
End                        #until this line of the file
something here...  
... ... ...  

我希望你明白我的意思.我正在考虑遍历文件,然后使用正则表达式搜索以找到开始"和结束"的行号,然后使用 linecache 从开始行读取到结束行.但是如何获取行号呢?我可以使用什么功能?

I hope you know what i mean. i was thinking of iterating through the file then search using regular expression to find the line number of "Start" and "End" then use linecache to read from Start line to End line. But how to get the line number? what function can i use?

推荐答案

如果你只是想要 StartEnd 之间的文本块,你可以做一些简单的事情,比如:

If you simply want the block of text between Start and End, you can do something simple like:

with open('test.txt') as input_data:
    # Skips text before the beginning of the interesting block:
    for line in input_data:
        if line.strip() == 'Start':  # Or whatever test is needed
            break
    # Reads text until the end of the block:
    for line in input_data:  # This keeps reading the file
        if line.strip() == 'End':
            break
        print line  # Line is extracted (or block_of_lines.append(line), etc.)

实际上,您不需要操纵行号来读取开始和结束标记之间的数据.

In fact, you do not need to manipulate line numbers in order to read the data between the Start and End markers.

逻辑(读取直到……")在两个块中重复,但它非常清晰和高效(其他方法通常涉及检查某些状态[到达块之前/块内/块结束],这会导致时间罚).

The logic ("read until…") is repeated in both blocks, but it is quite clear and efficient (other methods typically involve checking some state [before block/within block/end of block reached], which incurs a time penalty).

这篇关于python - 从和读取特定文本行的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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