This article describes how to process csv data in python. examples of how to process csv data in Python are analyzed. For more information about how to process csv data in python, see the following example. Share it with you for your reference. The details are as follows:
Python code:
The code is as follows:
# 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 ("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] = 'tabo ':
# 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 & gt; 600000"
Continue
If (int (ms_local)-int (ms_localup) + tm) <0:
Print 'rong1'
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 ()
The code is as follows:
# 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
Def median (lst ):
Even = (0 if len (lst) % 2 else 1) + 1
Half = (len (lst)-1)/2
Return sum (sorted (lst) [half: half + even])/float (even)
Def mean (lst ):
If len (lst) = 0:
Return 0
Return sum (lst)/len (lst)
NightLine = "21:01:00"
MorningLine = "09:01:00"
Def getTheDate (date ):
[FilenamePart1, filenamePart2] = string. split (filename ,'.')
[FilenamePart11, filenamePart12, filenamePart13] = string. split (filenamePart1 ,'_')
Return filenamePart13
Def afterOneMin (time ):
[Tm_local, ms_local] = string. split (time ,'.')
[Ymd, hms] = string. split (tm_local ,'')
Flag = 0
If hms> "21:01:00 ":
Flag = 1
Elif hms> "09:01:00" and hms <"20:00:00 ":
Flag = 1
Elif hms> "00:00:00" and hms <"05:00:00 ":
Flag = 1
Return flag
Rootdir = "/nethome/dehua. li/orderlifeMyWork/xingzheng/csv"
# CsvfileMaxMin = open ('E: \ dehua. li \ csv \__ xingzhenMaxMin.csv ', 'wb ')
# Writer1 = csv. writer (csvfileMaxMin)
# Aggregate (['feedcode', 'date', 'sentmaxtime', 'sentmaxlocaltime', 'sentmintime', 'sentminlocaltime', 'sentmeantime', 'sentmediance', 'ackedmaxtime ', 'ackedmaxlocaltime', 'ackedmintime', 'ackedminlocaltime', 'ackedmeantime', 'ackedmediantime', 'exchange '])
# Dimensions (['feedcode', 'date', 'sentmaxtime', 'sentmintime', 'sentmeantime', 'sentmedianc', 'ackedmaxtime', 'ackedmintime', 'ackedmeantime ', 'ackedmediantime', 'exchange '])
Msg = []
CodeList = list ()
OrderList = list ()
ItemSentList = []
ItemAckedList = []
FeedCode = []
Exchange = []
ZhengshangSentMedian = 0
ZhengshangSentMean = 0
ZhengshangAckedMedian = 0
ZhengshangAckedMean = 0
DashangSentMedian = 0
DashangSentMean = 0
DashangAckedMedian = 0
DashangAckedMean = 0
ShangqiSentMedian = 0
ShangqiSentMean = 0
ShangqiAckedMedian = 0
ShangqiAckedMean = 0
ZhongjinSentMedian = 0
ZhongjinSentMean = 0
ZhongjinAckedMedian = 0
ZhongjinAckedMean = 0
ZhengshangSent = []
ZhengshangAcked = []
DashangSent = []
DashangAcked = []
ShangqiSent = []
ShangqiAcked = []
ZhongjinSent = []
ZhongjinAcked = []
ZhengshangSentMedianAll = []
ZhengshangSentMeanAll = []
ZhengshangAckedMedianAll = []
ZhengshangAckedMeanAll = []
DashangSentMedianAll = []
DashangSentMeanAll = []
DashangAckedMedianAll = []
DashangAckedMeanAll = []
ShangqiSentMedianAll = []
ShangqiSentMeanAll = []
ShangqiAckedMedianAll = []
ShangqiAckedMeanAll = []
ZhongjinSentMedianAll = []
ZhongjinSentMeanAll = []
ZhongjinAckedMedianAll = []
ZhongjinAckedMeanAll = []
Zhengshang = '0'
Dashang = '0'
Shangqi = '0'
Zhongjin = '0'
With open ('/nethome/dehua. li/orderlifeCycleData/heyue_150128.csv', 'RB') as csvfile:
Reader = csv. reader (csvfile)
CsvItem = [row for row in reader]
Zhengshang = csvItem [2, 300] [3]
Dashang = csvItem [5] [3]
Shangqi = csvItem [165] [3]
Zhongjin = csvItem [2, 435] [3]
# For item in csvItem:
# If item [3] = zhengshang:
# Print item
For parent, dirname, filenames in OS. walk (rootdir ):
For filename in filenames:
FileName = OS. path. join (rootdir, filename)
Csvfile1 = open (fileName, 'RB ')
Reader = csv. reader (csvfile1)
CsvItem = [row for row in reader]
For item in CsvItem:
If item [0] = 'feedcode ':
Continue
If item [0] not in codeList:
CodeList. append (item [0])
# Print CsvItem [15]
If len (item) <= 5:
Print fileName
Print item
Print '++'
# If afterOneMin (item [3]) = 0:
# Print item [3]
# Continue
If item [5] = zhengshang and item [2] = 'sent ':
ZhengshangSent. append (int (item [4])
Elif item [5] = zhengshang and item [2] = 'acked ':
ZhengshangAcked. append (int (item [4])
Elif item [5] = dashang and item [2] = 'sent ':
DashangSent. append (int (item [4])
Elif item [5] = dashang and item [2] = 'acked ':
DashangAcked. append (int (item [4])
Elif item [5] = shangqi and item [2] = 'sent ':
ShangqiSent. append (int (item [4])
If int (item [4]) & gt; = 600000:
Print "------------"
Print item
Elif item [5] = shangqi and item [2] = 'acked ':
ShangqiAcked. append (int (item [4])
Elif item [5] = zhongjin and item [2] = 'sent ':
ZhongjinSent. append (int (item [4])
Elif item [5] = zhongjin and item [2] = 'acked ':
ZhongjinAcked. append (int (item [4])
Else:
Print "wrong info"
Print item
If mean (shangqiSent)> 420000:
Print sum (shangqiSent)
Print len (shangqiSent)
Print item
Print fileName
Print shangqiSent
ZhengshangSentMedian = median (zhengshangSent)
ZhengshangSentMean = mean (zhengshangSent)
ZhengshangAckedMedian = median (zhengshangAcked)
ZhengshangAckedMean = mean (zhengshangAcked)
DashangSentMedian = median (dashangSent)
DashangSentMean = mean (dashangSent)
DashangAckedMedian = median (dashangAcked)
DashangAckedMean = mean (dashangAcked)
ShangqiSentMedian = median (shangqiSent)
ShangqiSentMean = mean (shangqiSent)
ShangqiAckedMedian = median (shangqiAcked)
ShangqiAckedMean = mean (shangqiAcked)
ZhongjinSentMedian = median (zhongjinSent)
ZhongjinSentMean = mean (zhongjinSent)
ZhongjinAckedMedian = median (zhongjinAcked)
ZhongjinAckedMean = mean (zhongjinAcked)
# If mean (shangqiSent)> 70:
# Print '=================================='
# Print fileName
# Print codeList
'''
For listItem in codeList:
ItemSentList = []
ItemAckedList = []
For item in CsvItem:
If item [0] = listItem and item [2] = 'sent ':
ItemSentList. append (int (item [4])
Exchange = item [5]
Elif item [0] = listItem and item [2] = 'acked ':
ItemAckedList. append (int (item [4])
# Print itemSentList
ItemMaxSent = max (itemSentList)
ItemMinSent = min (itemSentList)
ItemAvgSent = sum (itemSentList)/len (itemSentList)
ItemMaxAcked = max (itemAckedList)
ItemMinAcked = min (itemAckedList)
ItemAvgAcked = sum (itemAckedList)/len (itemAckedList)
SentMedian = median (itemSentList)
AckedMedian = median (itemAckedList)
Msg = []
Msg. append (listItem) #0
Msg. append ("2015/01/14") #1
Msg. append (itemMaxSent) #2
Msg. append (itemMinSent) #3
Msg. append (itemAvgSent) #4
Msg. append (SentMedian) #5
Msg. append (itemMaxAcked) #6
Msg. append (itemMinAcked) #7
Msg. append (itemAvgAcked) #8
Msg. append (AckedMedian) #9
Msg. append (exchange) #10
If len (msg)> 15:
Print "------------------------------"
Print msg
Writer1.writerow (msg)
'''
ZhengshangSentMedianAll. append (zhengshangSentMedian)
ZhengshangSentMeanAll. append (zhengshangSentMean)
ZhengshangAckedMedianAll. append (zhengshangAckedMedian)
ZhengshangAckedMeanAll. append (zhengshangAckedMean)
DashangSentMedianAll. append (dashangSentMedian)
DashangSentMeanAll. append (dashangSentMean)
DashangAckedMedianAll. append (dashangAckedMedian)
DashangAckedMeanAll. append (dashangAckedMean)
ShangqiSentMedianAll. append (shangqiSentMedian)
ShangqiSentMeanAll. append (shangqiSentMean)
ShangqiAckedMedianAll. append (shangqiAckedMedian)
ShangqiAckedMeanAll. append (shangqiAckedMean)
ZhongjinSentMedianAll. append (zhongjinSentMedian)
ZhongjinSentMeanAll. append (zhongjinSentMean)
ZhongjinAckedMedianAll. append (zhongjinAckedMedian)
ZhongjinAckedMeanAll. append (zhongjinAckedMean)
Plt. figure (1)
Plt. figure (2)
Plt. figure (3)
Plt. figure (4)
Plt. figure (1)
Plt. title ('sentmean r-zhengshang B-dashang, green-shangqi gray-zhongjin ')
Plt. plot (range (1, len (zhengshangSentMeanAll) + 1), zhengshangSentMeanAll, 'r ')
Plt. plot (range (1, len (dashangSentMeanAll) + 1), dashangSentMeanAll, 'B ')
Plt. plot (range (1, len (shangqiSentMeanAll) + 1), shangqiSentMeanAll, 'g ')
Plt. plot (range (1, len (zhongjinSentMeanAll) + 1), zhongjinSentMeanAll, 'gray ')
Plt. savefig ('/nethome/dehua. li/orderlifeMyWork/xingzheng/data_noTPO_in10minutes/SentMean.png ')
Plt. figure (2)
Plt. title ('sentmedian r-zhengshang B-dashang, green-shangqi gray-zhongjin ')
Plt. plot (range (1, len (zhengshangSentMedianAll) + 1), zhengshangSentMedianAll, 'r ')
Plt. plot (range (1, len (dashangSentMedianAll) + 1), dashangSentMedianAll, 'B ')
Plt. plot (range (1, len (shangqiSentMedianAll) + 1), shangqiSentMedianAll, 'g ')
Plt. plot (range (1, len (zhongjinSentMedianAll) + 1), zhongjinSentMedianAll, 'gray ')
Plt. savefig ('/nethome/dehua. li/orderlifeMyWork/xingzheng/data_noTPO_in10minutes/SentMedian.png ')
Plt. figure (3)
Plt. title ('ackedmean r-zhengshang B-dashang, green-shangqi gray-zhongjin ')
Plt. plot (range (1, len (zhengshangAckedMeanAll) + 1), zhengshangAckedMeanAll, 'r ')
Plt. plot (range (1, len (dashangAckedMeanAll) + 1), dashangAckedMeanAll, 'B ')
Plt. plot (range (1, len (shangqiAckedMeanAll) + 1), shangqiAckedMeanAll, 'g ')
Plt. plot (range (1, len (zhongjinAckedMeanAll) + 1), zhongjinAckedMeanAll, 'gray ')
Plt. savefig ('/nethome/dehua. li/orderlifeMyWork/xingzheng/data_noTPO_in10minutes/AckedMean.png ')
Plt. figure (4)
Plt. title ('ackedmedian r-zhengshang B-dashang, green-shangqi gray-zhongjin ')
Plt. plot (range (1, len (zhengshangAckedMedianAll) + 1), zhengshangAckedMedianAll, 'r ')
Plt. plot (range (1, len (dashangAckedMedianAll) + 1), dashangAckedMedianAll, 'B ')
Plt. plot (range (1, len (shangqiAckedMedianAll) + 1), shangqiAckedMedianAll, 'g ')
Plt. plot (range (1, len (zhongjinAckedMedianAll) + 1), zhongjinAckedMedianAll, 'gray ')
Plt. savefig ('/nethome/dehua. li/orderlifeMyWork/xingzheng/data_noTPO_in10minutes/AckedMedian.png ')
Plt. show ()
Print 'over'
I hope this article will help you with Python programming.