我正在尝试修改脚本。这对我来说很难,所以我来寻求帮助。此脚本应从一些.out
文件中提取数据,然后将其写入.txt
文件中。问题是我有两个不同的关键字要查找。所以,我提供脚本,我无法修改的东西,然后是输入文件的两个例子。
#!/usr/bin/env python
# -*- coding: utf-8
#~ Data analysis
import glob, subprocess, shutil, os, math
from funciones import *
for namefile in glob.glob("*.mol2"):
lstmol2 = []
lstG=[]
os.chdir("some_directory")
searchprocess="grep -i -H 'CURRENT VALUE OF HEAT OF FORMATION =' *.out | sort -k 4 > firstfile.txt"
#~I need also to look for 'CURRENT BEST VALUE OF HEAT OF FORMATION ='
os.system(searchprocess)
fileout=open("results.txt","w")
filein=open("firstfile.txt", "r")
#~ write data in results.txt
fileout.write('\t %s \n' %(" HOF"))
for line in filein:
linediv=line.split()
HOF=float(linediv[8])
#~or [10] (for the keyword in need to add) but in both cases I need the float. I need both data of the keywords be included on this file.
lstG.append(HOF)
fileout.close()
filein.close()
输入数据,输入1:
foofoofooofoofoofoofoofoo
foofoofooofoofoofoofoofoov
foofoofooofoofoofoofoofoo
CURRENT VALUE OF HEAT OF FORMATION = 1928
foofoofooofoofoofoofoofoo
foofoofooofoofoofoofoofoov
输入数据,输入2:
foofoofooofoofoofoofoofoo
foofoofooofoofoofoofoofoov
foofoofooofoofoofoofoofoo
CURRENT BEST VALUE OF HEAT OF FORMATION = 1930
foofoofooofoofoofoofoofoo
foofoofooofoofoofoofoofoov
答案 0 :(得分:2)
您应该更新grep
命令,以便使用?
运算符查找可选字。使用-E
标志启用扩展正则表达式,这样您就不必转义正则表达式运算符。始终在模式周围使用单引号:
searchprocess="grep -E -i -H 'CURRENT( BEST)? VALUE OF HEAT OF FORMATION =' *.out | sort -k 4 > firstfile.txt"
@PrestonHager是正确的,您应该将linediv[8]
更改为linediv[-1]
,因为在存在BEST的情况下,该数字将位于linediv[9]
位置,但在这两种情况下{ {1}}将为您提供所需的结果。