Python - 将2个文件与CSV模块和3列进行比较

时间:2017-09-26 14:05:06

标签: python csv python-3.5

我正在尝试将2个文件与Python CSV模块进行比较。

第一个CSV文件,如下所示:

Name N, Age, Pound
A, 1, 10
A, 2, 10
B, 2, 12

第二个CSV文件:

Name N, Age, Pound
A, 1, 10
A, 3, 8
C, 1, 7

如果列名称和列Age相等,则返回输出中两个文件的磅。

输出文件示例:

Name, Age, Pound1, Pound2
A, 1, 10, 10

在Python 3.5代码中:

import csv
import io

alist, blist = [], []

with open("hosts.csv", "r") as fileA:
    reader = csv.reader(fileA, delimiter=',')
    for row in reader:
        alist.append(row)
with open("masterlist.csv", "r") as fileB:
    reader = csv.reader(fileB, delimiter=',')
    for row in reader:
        blist.append(row)

first_set = set(map(tuple, alist))
secnd_set = set(map(tuple, blist))

matches = set(first_set).intersection(secnd_set)
print (matches)

但只返回标题......

感谢。

2 个答案:

答案 0 :(得分:0)

你可以试试这个:

import csv
file1 = list(csv.reader(open('first.csv')))
file2 = list(csv.reader(open('second.csv')))
new_columns = [[a[0], a[1], a[-1], b[-1]] for a, b in zip(file1[1:], file2[1:]) if a[:2] == b[:2]]
write = csv.writer(open('outputfile.csv'))
write.writerow(new_columns)

答案 1 :(得分:0)

一种简单的方法是使用pandas:

import pandas as pd

# Load CSV files
df1 = pd.read_csv('first.csv')
df2 = pd.read_csv('second.csv')

# Merge tables
df = df1.merge(df2, on=['Name', 'Age'])

   Name   Age  Pound_x  Pound_y
0  A   1      10       10

# Rename Pound columns
df.rename(columns={'Pound_x': 'Pound1', 'Pound_y': 'Pound2'}, inplace=True)

   Name   Age  Pound1  Pound2
0  A   1      10       10

# Write output to file
df.to_csv('output.csv', index=False)