下面的代码稍微不那么优雅,但比依赖regex的David的更透明。它强烈依赖于您所描述的特定格式。另外,在我看来,实际上有6个(不是5个)变量你关心,除非你能把读到的比率转换成小数。在
您需要在nameList中提供正确的文件名列表(如果命名不方便,请手动)。在
另外,我不输出到excel文件而是输出到csv。当然,在Excel中打开csv文件非常简单,从中可以保存为xls。在
根据评论进行编辑(2013年5月19日):包括完整路径很简单。在import csv
import string
# Make list of all 20 files like so:
nameList = ['/full/path/to/Log.txt', '/different/path/to/Log.txt', '/yet/another/path/to/Log.txt']
lineNum = 68
myCols = ['nodes','n50','max','total','reads1','reads2']
myData = []
for name in nameList:
fi = open(name,"r")
table = string.maketrans("","")
# split line lineNum into list of strings
strings = fi.readlines()[lineNum-1].split()
# remove punctuation appropriately
nodes = int(strings[3])
n50 = int(strings[8].translate(table,string.punctuation))
myMax = int(strings[10].translate(table,string.punctuation))
total = int(strings[12].translate(table,string.punctuation))
reads1 = int(strings[14].split('/')[0])
reads2 = int(strings[14].split('/')[1])
myData.append([nodes, n50, myMax, total, reads1, reads2])
# Write the data out to a new csv file
fileOut = "out.csv"
csvFileOut = open(fileOut,"w")
myWriter = csv.writer(csvFileOut)
myWriter.writerow(myCols)
for line in myData:
myWriter.writerow(line)
csvFileOut.close()