在 python 社区的帮助下,我开始学习 python 来处理大约 5 亿(40G)的数据并编写了以下脚本。
输入文件格式-
Studentid,Subject,DateTime,Grade
001,Biology,Mon Apr 25 19:32:00 PDT 2013,B
001,Literature,Wed Apr 10 15:31:00 PST 2013,B
001,Math,Mon Apr 22 01:32:00 PDT 2013,A
002,Biology,Mon Apr 25 19:32:00 PDT 2013,A
002,Math,Mon Apr 22 16:31:14 PDT 2013,C
002,Math,Wed Apr 10 15:31:00 PST 2013,C
003,Biology,Mon Apr 22 13:31:00 PDT 2013,A
003,Irdu,Wed Apr 10 15:31:00 PST 2013,A
输出报告
003,Irdu;Wed Apr 10 15:31:00 PST 2013;A#Biology;Mon Apr 22 13:31:00 PDT 2013;A
002,Math;Wed Apr 10 15:31:00 PST 2013;C#Math;Mon Apr 22 16:31:14 PDT 2013;C#Biology;Mon Apr 25 19:32:00 PDT 2013;A
001,Literature;Wed Apr 10 15:31:00 PST 2013;B#Math;Mon Apr 22 01:32:00 PDT 2013;A#Biology;Mon Apr 25 19:32:00 PDT 2013;B
Python 脚本
import csv
import time
import operator
import sys, getopt
import os
from collections import defaultdict
from datetime import datetime
from operator import itemgetter
start = time.time()
def elapsed():
return time.time() - start
def date_key(row):
try:
formatRow = row[1].replace('PDT ','')
formatRow = formatRow.replace('PST ','')
return datetime.strptime(formatRow, "%a %b %d %X %Y")
except Exception, e:
print ("Error in sorting the date: %s \nRow : %s" % (e, row))
pass
def processRecords(accountsData, fileName):
for v in accountsData.itervalues():
try:
v.sort(key=date_key)
except Exception, e:
pass
with open(fileName, 'a') as writer:
for pid,v in accountsData.iteritems():
csv = '#'.join([';'.join(t) for t in v])
writer.write("%s,%s\n" % (pid, csv))
def main(argv):
inputFile = ''
outputFile = ''
batchsize = 20000000
try:
opts, args = getopt.getopt(argv,"hi:o:b:",["ifile=","ofile=","bsize="])
except getopt.GetoptError:
print 'ReportToFileBatches.py -i <inputfile> -o <outputfile> -b<batchsize>[default=20000000]'
sys.exit(2)
for opt, arg in opts:
if opt == '-h':
print 'ReportToFileBatches.py -i <inputfile> -o <outputfile> -b<batchsize>[default=20000000]'
sys.exit()
elif opt in ("-i", "--ifile"):
inputFile = arg
elif opt in ("-o", "--ofile"):
outputFile = arg
elif opt in ("-b", "--bsize"):
batchsize = int(arg)
if not (os.path.isfile(inputFile)):
print ("\nError : File - %s does not exist." % (inputFile))
sys.exit(2)
#print "Batch Size %s " % batchsize
linenumb = 0
with open(inputFile,'r') as data:
accounts = defaultdict(list)
for line in data:
linenumb = linenumb + 1
line = line.rstrip('\r\n')
try:
sid, subject, datetime, grade = line.split(',')
accounts[sid].append((subject, datetime, grade))
if (linenumb == batchsize):
linenumb = 0
processRecords(accounts, outputFile)
accounts = defaultdict(list)
else: continue
except Exception, e:
print ("Error : %s \nRow : %s" % (e, line))
if(linenumb > 0):
processRecords(accounts, outputFile)
print("Total time taken - %.3fs" % elapsed())
if __name__ == "__main__":
main(sys.argv[1:])
您可以看到输出文件(报告)是按日期排序的,也可以是字段的连接。我花更多时间对日期时间列进行排序(也许)。我是 Python 的新手。我非常感谢在改进我的脚本以减少处理时间方面的任何帮助。希望我说得通。
仅供参考:我确保输入文件按 studentid 排序并分批处理。