Reputation: 101
I want to read a file.txt in python and then print a specific position,I have two parties of the "ORBITAL ENERGIES", but I am interested only by the last, and especially, the line that corresponds 2.0000 and 0.0000. the input file here is:
---------------- ORBITAL ENERGIES ----------------
NO OCC E(Eh) E(eV)
0 2.0000 -19.126728 -520.4647
1 2.0000 -1.009435 -27.4681
2 2.0000 -0.538078 -14.6418
3 2.0000 -0.388855 -10.5813
4 2.0000 -0.321100 -8.7376
5 0.0000 -0.008155 -0.2219
6 0.0000 0.044286 1.2051
7 0.0000 0.141342 3.8461
8 0.0000 0.144536 3.9330
9 0.0000 0.153520 4.1775
10 0.0000 0.168115 4.5746
---------------- ORBITAL ENERGIES ----------------
NO OCC E(Eh) E(eV)
0 2.0000 -19.127397 -520.4829
1 2.0000 -1.014381 -27.6027
2 2.0000 -0.530762 -14.4428
3 2.0000 -0.398228 -10.8363
4 2.0000 -0.322062 -8.7638
5 0.0000 -0.007625 -0.2075
6 0.0000 0.044967 1.2236
7 0.0000 0.140524 3.8238
8 0.0000 0.144488 3.9317
9 0.0000 0.157663 4.2902
10 0.0000 0.166347 4.5265
11 0.0000 0.221116 6.0169
I used this code:
#!/usr/bin/env python
import sys, os
# ------------- GET INPUT FILE ------------- #
if len(sys.argv) <= 1:
name = raw_input("Enter path to input: ")
else:
name = sys.argv[1]
fo = open(name, "r")
lines = fo.readlines()
fo.close()
# ------------------------------------------ #
# Define Variables
omega = 0
o_HOMO = []
o_LUMO = []
# Sort Input Into Lists
for line in lines:
line = line.split() # split lines into lists
if not line: # skip empty lines
continue
if len(line) >= 3:
if line[1] == '2.0000': # gather neutral occupied eigenvalues (if closed shell)
o_HOMO.append(line[3])
elif line[1] == '0.0000': # gather virtual orbitals
o_LUMO.append(line[3])
for idx, item in enumerate(o_HOMO): #HOMO
if idx == len(o_HOMO) - 1:
print(item)
for idx, item in enumerate(o_LUMO): #LUMO
if idx == len(o_LUMO) - 1:
print(item)
My result is like this:
-8.7638
6.0169
But I want a result like this:
-8.7638
-0.2075
Upvotes: 0
Views: 372
Reputation: 13651
Solution using Python 3
:
zero_flag = 0
two_val = 0
zero_val = 0
with open("data.txt","r") as fp:
line_list = fp.readlines()
middle_str = "---------------- ORBITAL ENERGIES ----------------\n"
pos = len(line_list) - line_list[::-1].index(middle_str) - 1
line_list = line_list[pos:]
line_list = [line.strip() for line in line_list if line.strip()!=""]
for line in line_list:
try:
ar = list(map(float,line.split()))
if ar[1] == 2:
two_val = ar[3]
if ar[1] == 0 and zero_flag == 0:
zero_val = ar[3]
zero_flag = 1
except:
pass
print(two_val)
print(zero_val)
Output:
-8.7638
-0.2075
Data.txt
file contains:
---------------- ORBITAL ENERGIES ----------------
NO OCC E(Eh) E(eV)
0 2.0000 -19.126728 -520.4647
1 2.0000 -1.009435 -27.4681
2 2.0000 -0.538078 -14.6418
3 2.0000 -0.388855 -10.5813
4 2.0000 -0.321100 -8.7376
5 0.0000 -0.008155 -0.2219
6 0.0000 0.044286 1.2051
7 0.0000 0.141342 3.8461
8 0.0000 0.144536 3.9330
9 0.0000 0.153520 4.1775
10 0.0000 0.168115 4.5746
---------------- ORBITAL ENERGIES ----------------
NO OCC E(Eh) E(eV)
0 2.0000 -19.127397 -520.4829
1 2.0000 -1.014381 -27.6027
2 2.0000 -0.530762 -14.4428
3 2.0000 -0.398228 -10.8363
4 2.0000 -0.322062 -8.7638
5 0.0000 -0.007625 -0.2075
6 0.0000 0.044967 1.2236
7 0.0000 0.140524 3.8238
8 0.0000 0.144488 3.9317
9 0.0000 0.157663 4.2902
10 0.0000 0.166347 4.5265
11 0.0000 0.221116 6.0169
Share if you problem to understand any part.
Upvotes: 1