python - 从特定文本行读取文件

标签 python file linecache

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

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 从开始行读取到结束行。 但是如何获得行号?我可以使用什么功能?

最佳答案

如果您只是想要 StartEnd 之间的文本 block ,您可以执行以下简单操作:

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.)

实际上,您无需操作行号即可读取开始和结束标记之间的数据。

逻辑(“读取直到...”)在两个 block 中重复,但它非常清晰和有效(其他方法通常涉及检查某些状态[在 block 之前/ block 内/到达 block 结束],这会产生时间罚款)。

关于python - 从特定文本行读取文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7559397/

相关文章:

Python ffmpeg 在 heroku 上不起作用,没有错误,但它只是在那一行卡住

Python,PyFITS,无法打开文件

php - 检查文件是否大于 1 MB

python - 用文本文件中的相应行替换列表列表中的数字

python - Python 中不常见的 OOP 的原因?

python - 如何在 GCP 数据流中使用 python 管道代码读取 BigQuery 表

python - 从文本文件中逐行读取键/值对,并将值/字典传递给 urlopen http 函数

Java I/O 到 .txt 文件

python - 如何将 linecache 与 unicode 一起使用?

ruby-on-rails - 为什么bundle install会尝试安装linecache19?