python3简单实现从csv⽂件中读取内容,并对内容进⾏分类统
新⼿python刚刚上路,在实际⼯作中遇到如题所⽰的问题,尝试使⽤python3简单实现如下,欢迎⾼⼿前来优化
import csv
#打开⽂件,⽤with打开可以不⽤去特意关闭file了,python3不⽀持file()打开⽂件,只能⽤open()
with open("dk0519_1.csv","r",encoding="utf-8") as csv_file:
#读取csv⽂件,返回的是迭代类型
read = ader(csv_file)
alist = []
bdict = []
cdict = []
tmp = []
python新手怎么兼职for i in read:
alist.append(i[0].split("\t"))
for a in alist[1:]:
print(a)
bdict.append((a[0].split("?")[0],a[1]))
for b in bdict:
num = int(b[1])
j = bdict.index(b) + 1
while j <= (len(bdict)-1) :
if b[0] not in tmp:
if b[0] == bdict[j][0]:
num += int(bdict[j][1])
j += 1
else:
j +=1
else:
j +=1
if b[0] not in tmp:
cdict.append((b[0],num))
else:
pass
tmp.append(b[0])
with open('re_dk0519_1.csv','w',encoding="utf-8") as write_csvfile:
writer = csv.writer(write_csvfile)
writer.writerows(cdict)

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。