赞
踩
背景:实现一个csv文件内容对比的脚本,输出相同的数据总数,以及输出不相同的数据
前期准备:
data_detail.csv的数据11条
- 2021-06-02,我好,1782,12,others,others,12102,7224,1486,327.72,21%,45.37
- 2021-06-02,你好,1782,24,others,others,8109,4041,782,180.97,19%,44.78
- 2021-06-02,他好,1782,294,others,others,7278,4358,675,152.87,15%,35.08
- 2021-06-02,它好,1782,106,others,others,5344,3308,624,144.05,19%,43.55
- 2021-06-02,踏好,1782,197,others,others,3494,2104,367,84.5,17%,40.16
- 2021-06-02,塌好,1782,99,others,others,3801,2324,374,84.09,16%,36.18
- 2021-06-02,你好,1782,207,others,others,2432,1460,308,72.09,21%,49.38
- 2021-06-02,我好,1782,188,others,others,2286,1231,200,44.61,16%,36.24
- 2021-06-02,沓好,1782,49,others,others,2315,1224,189,43.68,15%,35.69
- 2021-06-02,塔塔,1782,225,others,others,1558,958,158,35.85,16%,37.42
data.csv数据10条
- 2021-06-02,我好,1782,12,others,others,12102,7224,1486,327.72,21%,45.37
- 2021-06-02,你好,1782,24,others,others,8109,4041,782,180.97,19%,44.78
- 2021-06-02,他好,1782,294,others,others,7278,4358,675,152.87,15%,35.08
- 2021-06-02,它好,1782,106,others,others,5344,3308,624,144.05,19%,43.55
- 2021-06-02,踏好,1782,197,others,others,3494,2104,367,84.5,17%,40.16
- 2021-06-02,塌好,1782,99,others,others,3801,2324,374,84.09,16%,36.18
- 2021-06-02,沓好,1782,143,others,others,2793,2003,355,83.79,18%,41.83
- 2021-06-02,你好,1782,207,others1,others,2432,1460,308,72.09,21%,49.38
- 2021-06-02,我好,1782,188,others,others,2286,1231,200,44.61,16%,36.24
- 2021-06-02,沓好,1782,49,others,others,2315,1224,189,43.68,15%,35.69
- 2021-06-02,塔塔,1782,225,others,others,1558,958,158,35.85,16%,37.42
代码具体实现:
- #!/usr/bin/python
- # -*- coding: UTF-8 -*-
- import csv
-
-
- def compa ():
- with open("data_detail.csv","r", encoding="utf-8") as f :
- red = csv.reader(f)
- count = 0
- for r in red:
- flag = 0
- with open("data.csv",encoding="utf-8") as n:
- dat = csv.reader(n)
- for d in dat:
- if r == d:
- count = count+1
- flag=1
- break
-
- if flag == 0:
- print(r)
- print("相同的数据总共有:", count)
-
-
- if __name__ == "__main__":
- compa()
-
注:flag的作用是判断如果flag=0证明没有相同的数据,则输出不相同的数据
结果:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。