首页 > 代码库 > 处理csv文件

处理csv文件

<pre name="code" class="python">
#-------------------------------------------------------------------------------
# Name:        module1
# Purpose:
#
# Author:      maoyongx
#
# Created:     21/01/2015
# Copyright:   (c) maoyongx 2015
# Licence:     <your licence>
#encoding='utf-8'
#-------------------------------------------------------------------------------
from __future__ import division
import csv
import datetime,time
import sys


exceltotalline=0
fail=0
success=0
starttime=''
endtime=''
a=0
info=[]
infosub=[]
infoerr=[]
infoerrsub=[]
parameter=0.85
def main():
    global exceltotalline
    global fail
    global success
    global starttime
    global endtime
    global a
    global info
    global infosub
    global infoerr
    global infoerrsub


    try:
        #address=raw_input("please input file address :")
        #print address
        address = sys.argv[1]




    #------------------------
        csvfile = file(address, 'rb')
    except:
        if address =='':
            print "address can't null"
            exit()
        else:
            print "please input correct address,example:python csv_read.py C:\\aa.csv"
            exit()
    reader = csv.reader(csvfile)
    #print type(reader)
    for line in reader:
        a=a+1
    csvfile.close()


    #-----------------------
    csvfile = file(address, 'rb')


    reader = csv.reader(csvfile)


    infoerrsub.append("TC Name")
    infoerrsub.append("Start ")
    infoerrsub.append('End Time')
    infoerrsub.append("Pass Rate")
    infoerr.append(infoerrsub)
    #print infoerr,'\n\n'
    infoerrsub=[]




    for line in reader:
        #print type(line)  <type 'list'>
        #print line
        if '"total_state"'  in line or  '"1/1"' in line:
            if line[5]!='"1"':
                rate=line[5].replace('"','')


                if float(rate)<parameter:


                    infoerrsub.append(line[0])
                    infoerrsub.append(line[3].replace('"',''))
                    infoerrsub.append(line[4].replace('"',''))
                    infoerrsub.append(line[5])
                    infoerr.append(infoerrsub)
                    infoerrsub=[]




                    #print line[0],line[3].replace('"',''),line[4].replace('"',''),line[5]
            #print line


        exceltotalline=exceltotalline+1  #record excel line
        for line2 in range(len(line)):  #???????
            if exceltotalline==2:  #??????
                starttime=line[3]
            if exceltotalline==a:
                endtime=line[3]


            #print len(line[line2])
            #print line[line2]
            if line[line2]=='"FAIL"':
                fail=fail+1
            elif line[line2]=='"PASS"':
                success=success+1


        #if exceltotalline==5:
        #    break
        #    continue
    #print  fail,success,starttime,a,endtime
    starttime=starttime.split('.')[0]
    starttime=starttime.replace('"',"")
    starttime=starttime.replace(':',"")
    starttime=starttime.replace(' ','')
    starttime=datetime.datetime(int(starttime[0:4]),int(starttime[4:6]),int(starttime[6:8]),int(starttime[8:10]),int(starttime[10:12]),int(starttime[12:14]))
    #print starttime


    endtime=endtime.split('.')[0]
    endtime=endtime.replace('"',"")
    endtime=endtime.replace(':',"")
    endtime=endtime.replace(' ','')
    endtime=datetime.datetime(int(endtime[0:4]),int(endtime[4:6]),int(endtime[6:8]),int(endtime[8:10]),int(endtime[10:12]),int(endtime[12:14]))
    #print endtime


    mkt_starttime = time.mktime(starttime.timetuple())
    mkt_endtime= time.mktime(endtime.timetuple())
    delt_time=mkt_endtime-mkt_starttime


    durationtime=time.strftime("%H:%M:%S",time.gmtime(delt_time))
    #durationtime=delt_time/60




    #print exceltotalline
    csvfile.close()
    infosub.append('Start time:')
    infosub.append(starttime)
    info.append(infosub)
    infosub=[]
    infosub.append('Stop time')
    infosub.append(endtime)
    info.append(infosub)
    infosub=[]
    infosub.append('Duration')
    infosub.append(durationtime)
    info.append(infosub)
    infosub=[]
    infosub.append('Pass')
    infosub.append(success)
    info.append(infosub)
    infosub=[]
    infosub.append('Fail')
    infosub.append(fail)
    info.append(infosub)
    infosub=[]
    infosub.append('Passrate')
    passrate =str(success/(success+fail)*100)+'%'
    infosub.append(passrate)
    info.append(infosub)
    infosub=[]
    #print info,'\n\n\n\n'
    #print infoerr
    info=info+infoerr
    try:
        csvfile = file('c:\\result.csv', 'wb')
    except:
        print "Please have manager access to run CMD!"
        exit()
    writer = csv.writer(csvfile)
    writer.writerows(info)


    csvfile.close()
    """print 'Start time:',starttime
    print 'Stop time',endtime
    print 'duration:',durationtime
    print 'Pass:',success
    print 'Fail:',fail
    print 'Passrate :',success/(success+fail)*100,'%'
    """


if __name__ == '__main__':
    main()
    #raw_input()


处理csv文件