python处理csv文件里面得数据

import csv

firsrCol = []
with open('C:\\Users\\Administrator\\Desktop\\owid-covid-data.csv', 'r') as f:
reader = csv.reader(f)
result = list(reader)
for key in result:
# print(key)
firsrCol.append(key[1])
newData = [i for n, i in enumerate(firsrCol) if i not in firsrCol[:n]] # 数组去重

record = []
maxTime = ''
for ll in newData:
arr = []
for key in result:
if key[1] == ll:
bbbb = key[2].replace("-", "")
# print(bbbb)
arr.append(bbbb)
maxTime = max(arr)
obj = {
"name": ll,
"time": maxTime
}
print(obj)
record.append(obj)
print(record)
finallData = []
for rec in record:
for key in result:
time = rec["time"][:4] + '-' + str(rec["time"][4:6]) + '-' + str(rec["time"][6:8])
if rec["name"] == key[1] and time == key[2]:
finallData.append(key)

csvFile = open("C:\\Users\\Administrator\\Desktop\\owid-covid-data(1).csv", "w") # 创建csv文件
writer = csv.writer(csvFile) # 创建写的对象
# 先写入columns_name
writer.writerow(["continent", "location", "date", "new_cases_per_million"]) # 写入列的名称
# 写入多行用writerows #写入多行
writer.writerows(finallData) # writer.writerows([[1,a,b],[2,c,d],[3,d,e]])

print(finallData)
print(len(newData))
上一篇:Vlog 入门指南


下一篇:confluent-kafka demo