使用python实现俩个csv文件内容对比

背景:实现一个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证明没有相同的数据,则输出不相同的数据

结果:

  • 2
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值