本文主要是介绍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))
这篇关于python处理csv文件里面得数据的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!