python处理csv数据(不具备参考功能)

清华大佬耗费三个月吐血整理的几百G的资源,免费分享!....>>>

#coding=utf-8
__author__ = 'dehua.li'

from datetime import *
import datetime
import csv
import sys
import time
import string
import os
import os.path

import pylab as plt

rootdir='/nethome/dehua.li/orderlifeCycleData/xingzheng'
writeFileDir="/nethome/dehua.li/orderlifeMyWork/xingzheng/csv"
heyueFile="/nethome/dehua.li/orderlifeCycleData/heyue_150128.csv"
ms_acked="1"

msg=[]
ex=[]

def getTheDate(date):
    [filenamePart1,filenamePart2]=string.split(filename,'.')
    [filenamePart11,filenamePart12,filenamePart13]=string.split(filenamePart1,'_')
    return filenamePart13



LocalTime=datetime.datetime.fromtimestamp(time.mktime(time.strptime("2014-11-04 20:59:59","%Y-%m-%d %H:%M:%S")))
for parent,dirname,filenames in os.walk(rootdir):
    for filename in filenames:
        fileNameWrite=os.path.join(writeFileDir,filename)
        print fileNameWrite
        csvfile00=open(fileNameWrite,'wb')
        writer1=csv.writer(csvfile00)
        writer1.writerow(['FeedCode','OrderId','Status','LocalTime','Time','Exchange'])

        fileName=os.path.join(parent,filename)
        [filenamePart1,filenamePart2]=string.split(filename,'.')
        [filenamePart11,filenamePart12,filenamePart13]=string.split(filenamePart1,'_')
        #filenamePart11_filenamePart12_filenamePart13.filenamePart2:dongzheng_orderlifeCycleData_20150111.csv

        print fileName
        with open(fileName,'rb') as csvfile:
            reader=csv.reader(csvfile)
            CsvItem=[row for row in reader]
            for item in CsvItem:
                if item[3]=='TPO':
                    #print " filter TPO "
                    continue
                if item[12]=='Sent':
                    [tm_local,ms_local]=string.split(item[15],'.')
                    [tm_localup,ms_localup]=string.split(item[19],'.')
                    LocalTime=datetime.datetime.fromtimestamp(time.mktime(time.strptime(tm_local,"%Y-%m-%d %H:%M:%S")))
                    LocalUpdate=datetime.datetime.fromtimestamp(time.mktime(time.strptime(tm_localup,"%Y-%m-%d %H:%M:%S")))


                    tm=int(((LocalTime-LocalUpdate).seconds))*1000
                    ms_sent=str(int(ms_local)-int(ms_localup)+tm)
                    if int(ms_sent)>10*60*1000:
                        print "ms_sent>600000"
                        continue


                    if(int(ms_local)-int(ms_localup)+tm)<0:
                        print 'wrong1'
                    msg=[]
                    msg.append(item[0])
                    msg.append(item[1])
                    msg.append(item[12])
                    msg.append(item[15])
                    msg.append(ms_sent)

                    with open(heyueFile,'rb') as csvfile1:
                        reader=csv.reader(csvfile1)
                        CsvItem=[row for row in reader]
                        for Item in CsvItem:
                            if Item[1]==item[0]:
                                msg.append(Item[3])
                                writer1.writerow(msg)
                                #print 'write ok'
                                ex=Item[3]
                                break
                    csvfile1.close()
                    with open(fileName,'rb') as csvfile22:
                        reader=csv.reader(csvfile22)
                        CsvItem2=[row for row in reader]
                        for item_ in CsvItem2:
                            if item_[12]=='Acked' and item_[1]==item[1]:
                                [tm_local2,ms_local2]=string.split(item_[15],'.')
                                LocalTime2=datetime.datetime.fromtimestamp(time.mktime(time.strptime(tm_local2,"%Y-%m-%d %H:%M:%S")))
                                tm2=int(((LocalTime2-LocalTime).seconds))*1000
                                ms_acked=str(int(ms_local2)-int(ms_local)+tm2)
                                if int(ms_acked)>10*60*1000:
                                    print "MSacked>600000"
                                    continue

                                msg=[]
                                msg.append(item_[0])
                                msg.append(item_[1])
                                msg.append(item_[12])
                                msg.append(item_[15])
                                msg.append(ms_acked)
                    with open(heyueFile,'rb') as csvfile111:
                        reader=csv.reader(csvfile111)
                        CsvItem=[row for row in reader]
                        for Item in CsvItem:
                            if Item[1]==item[0]:
                                msg.append(Item[3])
                                writer1.writerow(msg)
                                #print 'write ok'
                                break

                                #print "write ok"
                    csvfile22.close()
        csvfile.close()
        csvfile00.close()