python读取两个csv文件数据,进行查找匹配出现次数

现有需求 表1

表2

需要拿表1中的编码去表2中的门票编码列匹配,统计出现的次数,由于表2编码列是区域间,而且列不是固定的,代码如下

#encoding:utf-8
##导入两个CSV进行比对
import csv
##读取编码
def GetQrCode(filePath):
    #定义一个空的list集合
    list = set()
    csv_file = csv.reader(open(filePath, ‘r‘))
    for item in csv_file:
        # 判断字符串是否为纯数字
        if (item[0].isdigit()):
            ##将正常的编码存储到集合
            yield item[0]

def GetNumberList(filePath):
    csv_file = csv.reader(open(filePath, ‘r‘))
    #去掉表头
    flag=True
    for item in csv_file:
        if flag:
            # 去掉表头
            flag=False
            continue
        if len(item) > 1:
            yield item

if __name__ == ‘__main__‘:
     #定义字典装结果集
     dicList={}

    #获取编码列表
     qrcodelist=GetQrCode(‘E:\\new_qrcode.csv‘)
     #获取要匹配的号段list
     numberList=GetNumberList(‘E:\\门票统计汇总 - 副本.csv‘)
     isBreak=False
     for qrcode in qrcodelist:   ##最外层全部的qrcode
         #print(qrcode)
         #注意,这里要讲list转换为列表,否则循环过的元素不会重复进行
         numberList=list(numberList)
         for numbers in numberList: ##号段里的每一行
             if isBreak:
                 isBreak=False

                 break
             numbers=[i for i in numbers if i != ‘‘]
             numbers=list(numbers)
             for number in numbers:
                 if number!=numbers[0]: #单个号段
                     if number !=‘‘:
                         #判断一组还是两组
                         numberLength=len(str(number).split(‘-‘))
                         if numberLength>1:
                             #两组的
                             firstNumber=int(str(number).split(‘-‘)[0])
                             lastNumber=int(str(number).split(‘-‘)[1])
                             #print("first:"+str(firstNumber)+":"+str(lastNumber))
                             qrcode=int(qrcode)

                             if qrcode>=firstNumber and qrcode<=lastNumber:
                                 if numbers[0] in dicList.keys():
                                     dicList[numbers[0]]=int(dicList[numbers[0]])+1
                                 else:
                                     dicList[numbers[0]] =1
                                 isBreak=True
                                 break
                         else:
                             #一组的
                             #print(str(number))
                             firstNumber = int(number)

                             if qrcode==firstNumber:
                                 if numbers[0] in dicList.keys():
                                     dicList[numbers[0]]=int(dicList[numbers[0]])+1
                                 else:
                                     dicList[numbers[0]] =1
                                 isBreak = True
                                 break

    #讲结果集写入csv文件
     # 打开文件,追加a
     out = open(‘E:\\resut_1.csv‘, ‘a‘, newline=‘‘)

     # 设定写入模式
     csv_write = csv.writer(out, dialect=‘excel‘)
     # 写入具体内容
     csv_write.writerow(‘渠道名称‘,‘次数‘)

     for item in dicList:
         csv_write.writerow(item+‘,‘+str(dicList[item]))
         #print(str(item)+":"+ str(dicList[item]))
     out.close()
   

导出数据结果:

原文地址:https://www.cnblogs.com/dongml/p/10175542.html

时间: 2024-07-31 02:03:18

python读取两个csv文件数据,进行查找匹配出现次数的相关文章

python读取和写入csv文件

----------------python读取csv文件------------------ #导入csv 创建一个csv文件,输入内容 import csv #1.找到需要被打开的文件路径,通过open打开文件,声明打开的方式,声明编码格式 csv_file=open(r'csv文件路径', mode='r', encoding='utf-8') #2.通过csv模组提供的读取方法来读取打开的文件 csv_data=csv.reader(csv_file) #3.通过for循环遍历读取数据存

python读取并写入csv文件

新建csvData.csv文件 1 import csv 2 3 # 读取csv文件方式1 4 csvFile = open("csvData.csv", "r") 5 reader = csv.reader(csvFile) # 返回的是迭代类型 6 data = [] 7 for item in reader: 8 print(item) 9 data.append(item) 10 11 print(data) 12 csvFile.close() 13 14

C语言进行csv文件数据的读取

C语言进行csv文件数据的读取: #include <stdio.h> #include <string.h> #include <malloc.h> #include <stdlib.h> #include <math.h> int main(){ FILE *fp = NULL; char *line,*record; char buffer[20450];//20450这个数组大小也要根据自己文件的列数进行相应修改. if((fp = fo

用PHP读取Excel、CSV文件

PHP读取excel.csv文件的库有很多,但用的比较多的有: PHPOffice/PHPExcel.PHPOffice/PhpSpreadsheet,现在PHPExcel已经不再维护了,最新的一次提交还是在2017年12月25号,建议直接使用PhpSpreadsheet,而且这两个项目都是同一个组织维护的,本文介绍PhpSpreadsheet的使用. 介绍PhpSpreadsheet PhpSpreadsheet这个库是纯PHP写的,提供了非常丰富的类和方法,而且支持很多文件格式: 环境要求

PHP 读取和导出 CSV文件

PHP 读取和导出 CSV文件,速度比phpexcel快80%,而phpexcel 占内存,在数据大的情况下,5万条只需几秒不到,几乎感觉不出来 如果遇到数字是科学计算法,可以在前面加一个 ' 单引号 /** * 读取CSV文件 * @param string $csv_file csv文件路径 * @param int $lines 读取行数 * @param int $offset 起始行数 * @return array|bool */ public function read_csv_l

python读取txt、csv和excel文件

一.python读取txt文件:(思路:先打开文件,读取文件,最后用for循环输出内容) fp = open('test.txt','r') lines = fp.readlines() fp.close() for line in lines: username = line.split(',')[0] password = line.split(',')[1] 注:第一句是以只读方式打开文本文件:第二个是读取所有行的数据(read:读取整个文件:readline:读取一行数据):最后一定要关

python之读取和写入csv文件

写入csv文件源码: 1 #输出数据写入CSV文件 2 import csv 3 data = [ 4 ("Mike", "male", 24), 5 ("Lee", "male", 26), 6 ("Joy", "female", 22) 7 ] 8 9 #Python3.4以后的新方式,解决空行问题 10 with open('d://write.csv', 'w', newline

使用bash将csv文件数据读写到MySQL数据库的脚本之一

情景: 有一个包含多个系的学生详细信息的csv文件,需要将文件的内容插入到一个数据表中,保证每一个系生成一个单独的排名列表. 学习信息表 studentdata.csv 文件的数据如下: 1,Navin M,98,CS 2,Kavya N,70,CS 3,Nawaz O,80,CS 4,Hari S,80,EC 5,Alex M,50,EC 6,Neenu J,70,EC 7,Bob A,30,EC 8,Anu M,90,AE 9,Sruthi,89,AE 10,Andrew,89,AE 脚本思

Spark1.x和2.x如何读取和写入csv文件

看很多资料,很少有讲怎么去操作读写csv文件的,我也查了一些.很多博客都是很老的方法,还有好多转来转去的,复制粘贴都不能看.下面我在这里归纳一下,以免以后用到时再费时间去查 通过sc.textFile val input = sc.textFile("test.csv") val result = input.map { line => val reader = new CSVReader(new StringReader(line)); reader.readNext() }