Py学习  »  Python

在python中,在特定行后拆分文本文件

v.s.s • 5 年前 • 1324 次点击  

theta  sigma
1        0.1
2        0.1
3        0.2
...
END
some text...
theta   sigma
1        0.3
2        0.2
...
END
more data...

我想在每个“结束”之后生成一个新文件来分别分析数据。我尝试了其他答案的一些解决方案,比如

with open('fort.16', 'r') as infile, open('output_fort.16', 'w') as outfile:
copy= False
for line in infile:
    if line.strip() == '# legend':
        copy = True
        continue
    elif line.strip()=='End':
        copy = False
    elif copy:
        outfile.write(line)

但这不是我需要的。 我对python还不太熟悉,所以非常感谢您的帮助。

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/55591
 
1324 次点击  
文章 [ 2 ]  |  最新文章 5 年前