I'm reading in a large file and I want to write a certain portion of it to a new file.
The pattern 'CARTESIAN COORDINATES' appears twice in this file and I want to omit everything before the second occurrence in the new file. So far I have:
#!/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()
So I'm assuming the 101.out
file looks something like:
not to be written
not to be written
CARTESIAN COORDINATES
not to be written
CARTESIAN COORDINATES
written
written
written
And you want all the stuff after the second CARTESIAN COORDINATES
if I've understood you correctly:
#!/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 of those imports are required for the work, I've just left them there because they were in your original snippet.
This outputs to 101.xyz
:
written
written
written