如何查看python的csv数据

如题所述

# -*- coding:utf-8 -*-
import csv

def readbyday(csvfile,day):
csvcontent = csv.reader(file(csvfile, 'rb'))
res=[]
for line in csvcontent:
if line[0].find(day)<>-1:
res.append(line)
return res

def calcbyday(csvfile,day,flag):
#求某列数据的和及平均值

total=0
avg=0
res=readbyday(csvfile, day)
for i in res:
print i
total=total+int(i[flag])
avg=total/len(res)
return total,avg

date='2014-7-1'
flag=1 #falg=1/2/3 收入金额/支出金额/余额金额
total,avg=calcbyday(u'7月_1.csv',date,1)

print '%s 收入金额总数:%d\t平均:%d'%(date,total,avg)
输出:
['2014-7-1 0:01', '100', '80', '10000']
['2014-7-1 0:01', '101', '81', '10001']
['2014-7-1 0:08', '102', '82', '10002']
2014-7-1 收入金额总数:303 平均:101
温馨提示:答案为网友推荐,仅供参考
相似回答