当前位置:   article > 正文

使用python实现俩个csv文件内容对比_python csv文件中字段的值对比

python csv文件中字段的值对比

背景:实现一个csv文件内容对比的脚本,输出相同的数据总数,以及输出不相同的数据

前期准备:

data_detail.csv的数据11条

  1. 2021-06-02,我好,1782,12,others,others,12102,7224,1486,327.72,21%,45.37
  2. 2021-06-02,你好,1782,24,others,others,8109,4041,782,180.97,19%,44.78
  3. 2021-06-02,他好,1782,294,others,others,7278,4358,675,152.87,15%,35.08
  4. 2021-06-02,它好,1782,106,others,others,5344,3308,624,144.05,19%,43.55
  5. 2021-06-02,踏好,1782,197,others,others,3494,2104,367,84.5,17%,40.16
  6. 2021-06-02,塌好,1782,99,others,others,3801,2324,374,84.09,16%,36.18
  7. 2021-06-02,你好,1782,207,others,others,2432,1460,308,72.09,21%,49.38
  8. 2021-06-02,我好,1782,188,others,others,2286,1231,200,44.61,16%,36.24
  9. 2021-06-02,沓好,1782,49,others,others,2315,1224,189,43.68,15%,35.69
  10. 2021-06-02,塔塔,1782,225,others,others,1558,958,158,35.85,16%,37.42

data.csv数据10条

  1. 2021-06-02,我好,1782,12,others,others,12102,7224,1486,327.72,21%,45.37
  2. 2021-06-02,你好,1782,24,others,others,8109,4041,782,180.97,19%,44.78
  3. 2021-06-02,他好,1782,294,others,others,7278,4358,675,152.87,15%,35.08
  4. 2021-06-02,它好,1782,106,others,others,5344,3308,624,144.05,19%,43.55
  5. 2021-06-02,踏好,1782,197,others,others,3494,2104,367,84.5,17%,40.16
  6. 2021-06-02,塌好,1782,99,others,others,3801,2324,374,84.09,16%,36.18
  7. 2021-06-02,沓好,1782,143,others,others,2793,2003,355,83.79,18%,41.83
  8. 2021-06-02,你好,1782,207,others1,others,2432,1460,308,72.09,21%,49.38
  9. 2021-06-02,我好,1782,188,others,others,2286,1231,200,44.61,16%,36.24
  10. 2021-06-02,沓好,1782,49,others,others,2315,1224,189,43.68,15%,35.69
  11. 2021-06-02,塔塔,1782,225,others,others,1558,958,158,35.85,16%,37.42

代码具体实现:

  1. #!/usr/bin/python
  2. # -*- coding: UTF-8 -*-
  3. import csv
  4. def compa ():
  5. with open("data_detail.csv","r", encoding="utf-8") as f :
  6. red = csv.reader(f)
  7. count = 0
  8. for r in red:
  9. flag = 0
  10. with open("data.csv",encoding="utf-8") as n:
  11. dat = csv.reader(n)
  12. for d in dat:
  13. if r == d:
  14. count = count+1
  15. flag=1
  16. break
  17. if flag == 0:
  18. print(r)
  19. print("相同的数据总共有:", count)
  20. if __name__ == "__main__":
  21. compa()

注:flag的作用是判断如果flag=0证明没有相同的数据,则输出不相同的数据

结果:

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/340140
推荐阅读
相关标签
  

闽ICP备14008679号