我有一个带有(空格分隔的)数字的日志文件。问题在于,对于autoit (1gig文件、内存问题)来说,这个文件太大了,而且我看到了一些python的魔力。
我也想
1 2 3 4 5 6 7 8 9
1 2 3 A 7 8 9
目前我的regExp是
StringRegExpReplace($line, "(\S+)\s(\S+)\s(\S+)\s(\S+)\s(\S+)\s(\S+)\s(\S+)\s(\S+)\s(\S+)\s", "$1 $2 $3 0 $7 $8 $9")发布于 2011-03-06 03:26:50
无论如何,这里有一个Python解决方案:
with open('filename', 'rb') as infile:
with open('filename.out', 'wb') as outfile:
for j, line in enumerate(infile):
if j == 0: #skip first line
continue
listLine = line.split(' ')
listLine.insert(3, thingToInsert) #elements 4-6 are now elements 5-7
listLine = (el for i, el in enumerate(listLine) if i>4 and i<=7)
outfile.write(' '.join(listLine))这将是相当快,不需要太多的RAM,因为它读取和写入文件逐行。
发布于 2011-03-06 02:37:49
假设Windows,我可能会抓起一份gawk,并以比python更少的麻烦来完成它。
c:\gawk '{print $1 " " $2 " " $3 " added " $7 " " $8 " " $9}' bigfile.log >> outputfile.log免责声明:我没有Windows机器来测试这个,但是它可以在我目前使用的Linux机器上工作.
https://stackoverflow.com/questions/5208230
复制相似问题