我有一个文件,其中包含来自有机分子的几种能量。我想制作一个允许我打印选定能量的python脚本,比如说第一个或第二个或最后一个或最后一个之前的第三个等等。我正在学习python,我知道我应该做搜索列表,插入第二个参数,指示我要打印的能量。由于我正在学习,我不知道如何做到这一点。这是我制作的脚本,但第一个脚本只打印第一个能量,第二个脚本只打印最后一个。非常感谢任何帮助。(这是一个链接,其中包含我用作输入的文件示例https://dl.dropbox.com/u/63216126/ActCat.out)
我制作的第一个代码
import sys
import re
filename = sys.argv[1]
newfile = str(filename) + ".3d"
openold = open(filename,"r")
opennew = open(newfile,"w")
rline = openold.readlines()
for line in rline:
if re.search( r"SCF Done", line ):
words = line.split()
energy = float( words[4] )
print >>opennew, "The energy of the molecule is %f kcal mol-1" % energy
break
openold.close()
opennew.close()
我制作的第二个代码
import sys
import re
filename = sys.argv[1]
newfile = str(filename) + ".3d"
openold = open(filename,"r")
opennew = open(newfile,"w")
start = 0
rline = openold.readlines()
for i in range (len(rline)):
if "SCF Done" in rline[i]:
start = i
for line in rline[start:start+1] :
words = line.split()
energy = words[4]
print >>opennew, "The energy of the molecule is %s kcal mol-1" % energy
break
openold.close()
opennew.close()