切片输出文件

时间:2017-04-06 21:40:57

标签: python numpy scientific-computing

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

“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() 

1 个答案:

答案 0 :(得分:1)

所以我假设vector<unsigned>文件看起来像:

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)

101.xyz