python提取txt数据到excel_我需要从多个.txt文件中提取数据,并使用Python将它们移动到Excel文件中...

下面的代码稍微不那么优雅,但比依赖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()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值