切片输出文件

Slicing Output Files

我正在读取一个大文件,我想将其中的一部分写入一个新文件。

模式 'CARTESIAN COORDINATES' 在此文件中出现了两次,我想在新文件中省略第二次出现之前的所有内容。到目前为止我有:

#!/usr/bin/env python
import string,sys
import numpy as np
from numpy import *

with open("101.out", "r") as f:
    content = []
    for line in f:
        content.append(line)

g = open('101.xyz', 'w')


#write the relevant stuff


f.close() 

所以我假设 101.out 文件类似于:

not to be written 
not to be written
CARTESIAN COORDINATES
not to be written
CARTESIAN COORDINATES
written 
written
written

你想要第二个之后的所有内容 CARTESIAN COORDINATES 如果我没听错的话:

#!/usr/bin/env python
import string,sys
import numpy as np
from numpy import *

with open("101.out", "r") as f:
    content = []
    occurances = 0
    for line in f:
        if occurances >= 2: 
            content.append(line)
        if 'CARTESIAN COORDINATES' in line:
            occurances += 1

with open('101.xyz', 'w') as f:
    for item in content:
        f.write(item)

None 这些导入是这项工作所必需的,我只是将它们留在那里,因为它们在您的原始代码段中。

这输出到 101.xyz:

written
written
written