python处理csv数据的方法
本文实例讲述了python处理csv数据的方法。分享给大家供大家参考。具体如下:
Python代码:
代码如下:
#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()
代码如下:
#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)
#writer1.writerow(['FeedCode','date','SentMaxTime','SentMaxLocalTime','SentMinTime','SentMinLocalTime','SentMeanTime','SentMedian','AckedMaxTime','AckedMaxLocalTime','AckedMinTime','AckedMinLocalTime','AckedMeanTime','AckedMedianTime','Exchange'])
#writer1.writerow(['FeedCode','date','SentMaxTime','SentMinTime','SentMeanTime','SentMedian','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[300][3]
dashang=csvItem[5][3]
shangqi=csvItem[165][3]
zhongjin=csvItem[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])>=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 grey-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,'grey')
plt.savefig('/nethome/dehua.li/orderlifeMyWork/xingzheng/data_noTPO_in10minutes/SentMean.png')
plt.figure(2)
plt.title('SentMedian r-zhengshang b-dashang,green-shangqi grey-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,'grey')
plt.savefig('/nethome/dehua.li/orderlifeMyWork/xingzheng/data_noTPO_in10minutes/SentMedian.png')
plt.figure(3)
plt.title('AckedMean r-zhengshang b-dashang,green-shangqi grey-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,'grey')
plt.savefig('/nethome/dehua.li/orderlifeMyWork/xingzheng/data_noTPO_in10minutes/AckedMean.png')
plt.figure(4)
plt.title('AckedMedian r-zhengshang b-dashang,green-shangqi grey-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,'grey')
plt.savefig('/nethome/dehua.li/orderlifeMyWork/xingzheng/data_noTPO_in10minutes/AckedMedian.png')
plt.show()
print 'over'
希望本文所述对大家的Python程序设计有所帮助。
数据分析咨询请扫描二维码
若不方便扫码,搜微信号:CDAshujufenxi
AI来了,数分人也可以很省力,今天给大家介绍7个AI+数据分析工具,建议收藏。 01酷表 EXCEL 网址:https://chatexcel.com/ 这是 ...
2024-12-26一个好的数据分析模型不仅能使分析具备条理性和逻辑性,而且还更具备结构化和体系化,并保证分析结果的有效性和准确性。好的数据 ...
2024-12-26当下,AI 的发展堪称狂飙猛进。从 ChatGPT 横空出世到各种大语言模型(LLM)接连上线,似乎每个人的朋友圈都在讨论 AI 会不会“ ...
2024-12-26数据分析师这个职业已经成为了职场中的“香饽饽”,无论是互联网公司还是传统行业,都离不开数据支持。想成为一名优秀的数据分析 ...
2024-12-26在数据驱动决策成为商业常态的今天,数据分析师这一职业正迎来前所未有的机遇与挑战。很多希望转行或初入职场的人士不禁询问:数 ...
2024-12-25数据分析师,这一近年来炙手可热的职业,吸引了大量求职者的注意。凭借在大数据时代中的关键作用,数据分析师不仅需要具备处理数 ...
2024-12-25在当今数字化变革的浪潮中,数据分析师这一职业正迎来前所未有的发展机遇。回想我自己初入数据分析行业时,那种既兴奋又略显谨慎 ...
2024-12-25在当今信息爆炸的时代,数据已经像空气一样无处不在,而数据分析则是解锁这些信息宝藏的钥匙。数据分析的过程就像是一次探险,从 ...
2024-12-25在职场上,拍脑袋做决策的时代早已过去。数据分析正在成为每个职场人的核心竞争力,不仅能帮你找到问题,还能提供解决方案,提升 ...
2024-12-24Excel是数据分析的重要工具,强大的内置功能使其成为许多分析师的首选。在日常工作中,启用Excel的数据分析工具库能够显著提升数 ...
2024-12-23在当今信息爆炸的时代,数据分析师如同一位现代社会的侦探,肩负着从海量数据中提炼出有价值信息的重任。在这个过程中,掌握一系 ...
2024-12-23在现代的职场中,制作吸引人的PPT已经成为展示信息的重要手段,而其中数据对比的有效呈现尤为关键。为了让数据在幻灯片上不仅准 ...
2024-12-23在信息泛滥的现代社会,数据分析师已成为企业决策过程中不可或缺的角色。他们的任务是从海量数据中提取有价值的洞察,帮助组织制 ...
2024-12-23在数据驱动时代,数据分析已成为各行各业的必需技能。无论是提升个人能力还是推动职业发展,选择一条适合自己的学习路线至关重要 ...
2024-12-23在准备数据分析师面试时,掌握高频考题及其解答是应对面试的关键。为了帮助大家轻松上岸,以下是10个高频考题及其详细解析,外加 ...
2024-12-20互联网数据分析师是一个热门且综合性的职业,他们通过数据挖掘和分析,为企业的业务决策和运营优化提供强有力的支持。尤其在如今 ...
2024-12-20在现代商业环境中,数据分析师是不可或缺的角色。他们的工作不仅仅是对数据进行深入分析,更是协助企业从复杂的数据信息中提炼出 ...
2024-12-20随着大数据时代的到来,数据驱动的决策方式开始受到越来越多企业的青睐。近年来,数据分析在人力资源管理中正在扮演着至关重要的 ...
2024-12-20在数据分析的世界里,表面上的技术操作只是“入门票”,而真正的高手则需要打破一些“看不见的墙”。这些“隐形天花板”限制了数 ...
2024-12-19在数据分析领域,尽管行业前景广阔、岗位需求旺盛,但实际的工作难度却远超很多人的想象。很多新手初入数据分析岗位时,常常被各 ...
2024-12-19