Skip to main content
 首页 » 编程设计

python之我想在第一个空行开始解析文本文件

2025年05月04日10fff_TT

我想从文本文件的第一个空行开始解析文本文件。每个文本文件的前几行都有我不想在搜索中使用的 URL,并且每个文件的标题长度略有不同。每个文件在标题和文本正文之间都有一个空行,所以我想在空行之后开始我的正则表达式搜索

我知道如何找到空行,但不知道如何获取它们的索引。

myfile = open(mydir,'r') 
for line in myfile: 
    if line in ['\n', '\r\n']: 
        print 'Found it' 

感谢任何帮助

请您参考如下方法:

with open(mydir,'r') as myfile 
    next(line for line in myfile if line.isspace()) 
    # now myfile is at the first line after the blank line