
# -- coding:utf-8 --
import csv
def readbyday(csvfile,day):
csvcontent = csvreader(file(csvfile, 'rb'))
res=[]
for line in csvcontent:
if line[0]find(day)<>-1:
resappend(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月_1csv',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
估计是乱码造成,需要在读取的py文件中指定编码格式:
在文件开头,设置成gbk格式import
os,sys,re,csv
reload(sys)
syssetdefaultencoding('gbk')
以上就是关于如何查看python的csv数据全部的内容,包括:如何查看python的csv数据、python读取csv文件时,数据内容含有中文,读取出错,怎么办、等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)