Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何基于另一个CSV中的行删除一个CSV中的行,并使用特定列进行比较_Python_Loops_Csv_Compare - Fatal编程技术网

Python 如何基于另一个CSV中的行删除一个CSV中的行,并使用特定列进行比较

Python 如何基于另一个CSV中的行删除一个CSV中的行,并使用特定列进行比较,python,loops,csv,compare,Python,Loops,Csv,Compare,我有两个CSV文件,CSV1和CSV2,有多个列和行。其中一列的标题为ID 我想做的是检查CSV1的行,如果CSV2的ID列中有一个ID与CSV1的行ID匹配,那么我想保留该行。如果没有匹配项,那么我想从CSV1中删除该行 基本上,CSV1中的数据与我相关,但仅与CSV2中的人员相关。两者之间的唯一连接是ID列。所以我需要检查CSV1中的所有行,看看该行ID是否在CSV2中的一行中 这是我到目前为止所拥有的 import csv smarteeCSV = open("Smartee.csv",

我有两个CSV文件,CSV1和CSV2,有多个列和行。其中一列的标题为ID

我想做的是检查CSV1的行,如果CSV2的ID列中有一个ID与CSV1的行ID匹配,那么我想保留该行。如果没有匹配项,那么我想从CSV1中删除该行

基本上,CSV1中的数据与我相关,但仅与CSV2中的人员相关。两者之间的唯一连接是ID列。所以我需要检查CSV1中的所有行,看看该行ID是否在CSV2中的一行中

这是我到目前为止所拥有的

import csv
smarteeCSV = open("Smartee.csv", "r")
aeriesCSV = open("aeriesEditable.csv", "r+")

aeries = csv.reader(aeriesCSV, delimiter=',')##CSV1
smartee = csv.reader(smarteeCSV, delimiter=',')##CSV2    

for row in aeries:
    for item in smartee
    if row[1] != item[1]##indexes for the columns with the ids

我已经知道我没有在正确的轨道上,所以有人可以帮助吗?

您可以提取第二个文件中的所有ID,并在每次检查第一个文件的一行时查找这些ID

例如:

# extract ID column from CSV file 2 into a set
Ids = { row[1] for row in smartee }

# pick only rows whose ID is in Ids 
filtered_rows = [item for item in aeries if item[1] in Ids] 

您可以提取第二个文件中的所有ID,并在每次检查第一个文件的一行时查找这些ID

例如:

# extract ID column from CSV file 2 into a set
Ids = { row[1] for row in smartee }

# pick only rows whose ID is in Ids 
filtered_rows = [item for item in aeries if item[1] in Ids] 

首先,阅读CSV2,只生成一组ID:

with open(CSV2) as f:
    r = csv.DictReader(f)
    theids = set(row['ID'] for row in r)
然后,在读取CSV1时,只需检查ID是否在集合中:

with open(CSV1) as f, open(CSV1 + '.new', 'w') as out:
    r = csv.DictReader(r)
    w = csv.DictWriter(out, r.fieldnames)
    for row in r:
        if row['ID'] in theids:
            w.writerow(row)

这假设CSV文件适用于基于dict的读/写,即第一行是列名列表,但如果列名也来自其他信息,则很容易调整。

首先,读取CSV2以生成一组ID:

with open(CSV2) as f:
    r = csv.DictReader(f)
    theids = set(row['ID'] for row in r)
然后,在读取CSV1时,只需检查ID是否在集合中:

with open(CSV1) as f, open(CSV1 + '.new', 'w') as out:
    r = csv.DictReader(r)
    w = csv.DictWriter(out, r.fieldnames)
    for row in r:
        if row['ID'] in theids:
            w.writerow(row)

这假设CSV文件适合基于dict的读/写,即第一行是列名列表,但如果列名也来自其他信息,则很容易调整。

根据您计划对相关数据行执行的操作,您可能能够使用Python的内置函数来执行您需要的操作:

import csv

# first get the ids    
with open('Smartee.csv', 'rb') as smarteeCSV:  # CSV2
    ids = set(row['ID'] for row in csv.DictReader(smarteeCSV, delimiter=','))

with open('aeriesEditable.csv', 'rb') as aeriesCSV:  # CSV1
    relevant = filter(lambda row: if row['ID'] in ids,
                        csv.DictReader(aeriesCSV, delimiter=','))

# relevant will be a list containing the desired rows from CSV1

如果您想要迭代地处理这些行,那么对于第二部分,您可以使用for循环来替代类似地调用函数的结果

根据您计划对相关数据行执行的操作,您可能能够使用Python的内置函数来执行您需要的操作:

import csv

# first get the ids    
with open('Smartee.csv', 'rb') as smarteeCSV:  # CSV2
    ids = set(row['ID'] for row in csv.DictReader(smarteeCSV, delimiter=','))

with open('aeriesEditable.csv', 'rb') as aeriesCSV:  # CSV1
    relevant = filter(lambda row: if row['ID'] in ids,
                        csv.DictReader(aeriesCSV, delimiter=','))

# relevant will be a list containing the desired rows from CSV1

如果您想要迭代地处理这些行,那么对于第二部分,您可以使用for循环来替代类似地调用函数的结果

ID作为一个集合会更好,01相对于linearNo担心,对小的输入不会有影响,但对大的输入会更有效率。你们都是人类中的神!工作非常完美,完全符合我的要求。你太棒了!!ID作为一个集合会更好,01相对于linearNo担心,对小的输入不会有影响,但对大的输入会更有效率。你们都是人类中的神!工作非常完美,完全符合我的要求。你太棒了!!