【问题标题】:Python 2 csv files parser that outputs what is in file1.csv but not in file2.csvPython 2 csv 文件解析器,它输出 file1.csv 中的内容,但 file2.csv 中没有的内容
【发布时间】:2016-04-10 17:45:20
【问题描述】:

由于是我的第一篇文章,我想向大家问好,我很高兴能加入这个很棒的社区!我想学习编程,我决定从 python 开始,我已经有 2 个月的时间在 python 中,所以我处于初学者水平。

我的第一个挑战是帮助有以下情况的朋友,我可以理解基本的python功能,但我需要的脚本对于我的lvl来说有点复杂。

所以我有 2 个 csv 文件(file1.csv、file2.csv),如果该索引在 file1 中,我需要根据每行 file2.csv 中的索引 [codfiscal] 检查.csv 在任何行中,如果它没有打印 file3.csv 中的所有行。 为了举例更好地理解我的问题,我将发布一些示例:

输入:

file2.csv

Denumire;codfiscal;jreg;adresaCOR;adresaNLC;judet1;localitate;tipclient;stare;nu plateste la soc:;Stare firma;an_jreg

"""ACNORD""-P SRL";15444630;J2/614/2003; NR 14; NR 14;ARAD;SANMARTIN;PJ;FUNCTIUNE;ENEL;active;2003

"""ARBU""- TOY SRL";12766886;J2/122/2000;求救。 CURTICI- DOROBAN?I NR F.N.;求救。 CURTICI- DOROBAN?I NR F.N.;ARAD;CURTICI;PJ;FUNCTIUNE;ENEL;active;2000

(IN.A.P.S) INARCH PRMO STRUCTURES SRL;16420906;J2/844/2004; B-DUL 普通 VASILE MILEA NR 3 ET 2 AP 11; B-DUL GENERAL VASILE MILEA NR 3 ET 2 AP 11;ARAD;ARAD;PJ;FUNCTIUNE;ENEL;active;2004

?IRU CONSTRUCT SRL;22802765;J35/4342/2007; STR。 TESATORILOR NR 8A ET 4 AP 10; STR。 TESATORILOR NR 8A ET 4 AP 10;TIMIS;LUGOJ;PJ;FUNCTIUNE;ENEL;active;2007

?OLEA INTER-COM SRL;16918200;J2/1887/2004; NR 276; NR 276;ARAD;格罗塞尼;PJ;FUNCTIUNE;ENEL;活性;2004

file1.csv

Denumire;codfiscal;jreg;adresaCOR;adresaNLC;judet1;localitate;tipclient;stare;nu plateste la soc:;Stare firma;an_jreg

"""ACNORD""-P SRL";15444630;J2/614/2003; NR 14; NR 14;ARAD;SANMARTIN;PJ;FUNCTIUNE;ENEL;active;2003

"""ARBU""- TOY SRL";12766886;J2/122/2000;求救。 CURTICI- DOROBAN?I NR F.N.;求救。 CURTICI- DOROBAN?I NR F.N.;ARAD;CURTICI;PJ;FUNCTIUNE;ENEL;active;2000

(IN.A.P.S) INARCH PRMO STRUCTURES SRL;16420906;J2/844/2004; B-DUL 普通 VASILE MILEA NR 3 ET 2 AP 11; B-DUL GENERAL VASILE MILEA NR 3 ET 2 AP 11;ARAD;ARAD;PJ;FUNCTIUNE;ENEL;active;2004

.A.S.A. SERVICII ECOLOGICE SRL;14822567;J2/648/2002; ZONA CET - SOSEAUA CENTURA NORD NR FN; ZONA CET - SOSEAUA CENTURA NORD NR FN;ARAD;ARAD;PJ;FUNCTIUNE;ENEL;active;2002

?AIZAR CONS SRL;23957563;J11/441/2008; B-杜尔共和国 NR 25 SC 3 ET 8 AP 29; B-DUL REPUBLICII NR 25 SC 3 ET 8 AP 29;CARAS-SEVERIN;RESITA;PJ;FUNCTIUNE;ENEL;active;2008

?ICA - STRONG SRL;15528110;J2/745/2003; STR。 OCSKO TEREZIA BL 11 SC B ET 2 AP 6; STR。 OCSKO TEREZIA BL 11 SC B ET 2 AP 6;ARAD;ARAD;PJ;FUNCTIUNE;ENEL;active;2003

?IGHERTU CONSTRUCT SRL;19298294;J2/2238/2006; STR。 CEZAR NR 5; STR。 CEZAR NR 5;ARAD;ARAD;PJ;FUNCTIUNE;ENEL;active;2006

想要的输出:

file3.csv

Denumire;codfiscal;jreg;adresaCOR;adresaNLC;judet1;localitate;tipclient;stare;nu plateste la soc:;Stare firma;an_jreg

?IRU CONSTRUCT SRL;22802765;J35/4342/2007; STR。 TESATORILOR NR 8A ET 4 AP 10; STR。 TESATORILOR NR 8A ET 4 AP 10;TIMIS;LUGOJ;PJ;FUNCTIUNE;ENEL;active;2007

?OLEA INTER-COM SRL;16918200;J2/1887/2004; NR 276; NR 276;ARAD;格罗塞尼;PJ;FUNCTIUNE;ENEL;活性;2004

这是我的 csv 示例,对它们进行了一些编辑,以便您可以更好地查看它们(忽略空格)。

重要的是,我的真实数据就像 file1.csv 上的 100k 行和 file2.csv 上的 50k 行。所以脚本必须处理大数据。我在 xcell 文件上有这些数据,我认为使用 cvs 会更容易,然后再转换回来!

非常感谢您的宝贵时间,我们将不胜感激所有的帮助!

【问题讨论】:

    标签: python excel python-2.7 csv


    【解决方案1】:

    我会这样做:

    codfiscal_in_f1_set = set()
    with open("file1.csv", 'r') as f1:
        for line in f1:
            line = line.strip()
            if not line:
                continue
    
            codfiscal_in_f1_set.add(line.split(";")[1])
    
    with open("file2.csv", 'r') as f2, open("file3.csv", "w") as output_file:
        for line in f2:
            line = line.strip()
            if not line:
                continue
    
            codfiscal = line.split(";")[1]
            if codfiscal not in codfiscal_in_f1_set:
                output_file.write(line + "\n")
    

    【讨论】:

    • 哇,这很接近,file3 中的输出是来自 file2 和 file1 的公共元素,我需要 file2 中的内容,而不是 file1 中的内容。 File3 输出是带有“15444630”、“12766886”和“16420906”的输出,我需要在想要的输出上带有 22802765 和 16918200 的行。
    • Denumire;codfiscal;jreg;adresaCOR;adresaNLC;judet1;localitate;tipclient;stare;nu plateste la soc:;Stare firma;an_jreg """ACNORD""-P SRL";15444630;J2 /614/2003; NR 14; NR 14;ARAD;SANMARTIN;PJ;FUNCTIUNE;ENEL;active;2003 """ARBU""- TOY SRL";12766886;J2/122/2000;求救。 CURTICI- DOROBAN?I NR F.N.;求救。 CURTICI- DOROBAN?I NR F.N.;ARAD;CURTICI;PJ;FUNCTIUNE;ENEL;active;2000 (IN.A.P.S) INARCH PreMO STRUCTURES SRL;16420906;J2/844/2004; B-DUL 普通 VASILE MILEA NR 3 ET 2 AP 11; B-DUL GENERAL VASILE MILEA NR 3 ET 2 AP 11;ARAD;ARAD;PJ;FUNCTIUNE;ENEL;active;2004
    • 我需要 ?IRU CONSTRUCT SRL;22802765;J35/4342/2007; STR。 TESATORILOR NR 8A ET 4 AP 10; STR。 TESATORILOR NR 8A ET 4 AP 10;TIMIS;LUGOJ;PJ;FUNCTIUNE;ENEL;active;2007 ?OLEA INTER-COM SRL;16918200;J2/1887/2004; NR 276; NR 276;ARAD;格罗塞尼;PJ;FUNCTIUNE;ENEL;活跃;2004
    • 我已经编辑了解决方案以反映 cmets 中需要的更改。希望有效
    • 完美,太棒了,我可以在哪里了解更多关于你在该代码中所做的事情(我假设的集合)?谢谢!
    【解决方案2】:

    使用 Python 集。假设数字总是在第二列:

    #load CV2
    
    inCv2Set=set()
    with open("file2.csv",'r') as file2:
       data=file2.read().split(' ')
       numbers = map(lambda x: x.split(';')[1],data)
       inCv2Set=set(numbers)
    
    outFile=open("file3.csv")
    with open("file1.csv",'r') as file1:
       data=file1.read().split(' ')
       header=data[0]
       data=data[1:] # to remove head
       #loop over the data and insert if not in the set
       for d in data:
            if not d.split(';')[1] in inCv2Set:
                 outFile.write(d+'\n')
    outFile.close()
    

    如果您不知道位置,可以对头部进行进一步操作

    【讨论】:

    • 感谢您的回复,我在运行代码时收到此错误:Traceback(最近一次调用最后一次):文件“”,第 1 行,在 文件“C:\Anaconda2 \lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py”,第 699 行,在运行文件 execfile(filename, namespace) 文件“C:\Anaconda2\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize. py”,第 74 行,在 execfile 中 exec(compile(scripttext, filename, 'exec'), glob, loc) 文件“D:/proiect/csvwork/parser.py”,第 2 行,在 .csv",'r') as file2: AttributeError: exit
    • 如果我尝试导入 cv2 我得到这个 > D:\proiet\csvwork>parser.py RuntimeError: module compiled against API version 9 但这个版本的 numpy 是 6 Traceback(最近一次调用最后) :文件“D:\proiet\csvwork\parser.py”,第 1 行,在 import cv2 ImportError: numpy.core.multiarray failed to import
    • 对于导入 cv2 错误 .. 这是一个不同的问题。你应该明显地更新 numpy
    • 我现在在 anaconda 中:
    • 好,希望答案对你有所帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-02-09
    • 1970-01-01
    • 1970-01-01
    • 2018-11-18
    • 2021-08-10
    • 2017-05-16
    • 2021-04-30
    相关资源
    最近更新 更多