将CSV与不同的列合并

时间:2019-04-26 08:17:09

标签: python csv join merge

我有问题:

我有两个CSV文件

这是CSV1的内容:

Switch,Switchport Number,Port Name,VLAN
TEST-SWITCH,1,TEST PORT 1,20
TEST-SWITCH,2,DEVICE 1,20
TEST-SWITCH,3,TEST PORT 2,20
TEST-SWITCH,4,DEVICE 2,20
TEST-SWITCH,5,TEST PORT 3,20
TEST-SWITCH,6,DEVICE 3,20
TEST-SWITCH,7,OFFICE,20
TEST-SWITCH,8,FREE,20
TEST-SWITCH,9,THIN CLIENT,20
TEST-SWITCH,10,PRINTER,20
TEST-SWITCH,11,COUNTER 1,20
TEST-SWITCH,12,COUNTER 2,20
TEST-SWITCH,13,FREE,20
TEST-SWITCH,14,FREE,20
TEST-SWITCH,15,FREE,10
TEST-SWITCH,16,FREE,10
TEST-SWITCH,17,MUSIC PLAYER,10
TEST-SWITCH,18,VIDEO PLAYER,10
TEST-SWITCH,19,ALARM,10
TEST-SWITCH,20,AP1,99
TEST-SWITCH,21,AP2,99
TEST-SWITCH,22,AP3,99
TEST-SWITCH,23,AP4,99
TEST-SWITCH,24,UPLINK,99
TEST-SWITCH,25,Reserve,99
TEST-SWITCH,26,Reserve,99
TEST-SWITCH,27,Reserve,99
TEST-SWITCH,28,Reserve,99

以下是CSV2的内容:

Switch Port,IP Address,Mac Address,Manufactor
17,172.28.7.219,38:91:fb:00:1c:78,Xenox Holding BV
1,10.33.6.10,ec:b1:d7:61:b7:40,Hewlett Packard
9,10.33.6.30,7c:d3:0a:1b:5c:6e,INVENTEC CORPORATION
22,172.28.7.211,08:e6:89:96:f3:ac,Apple Inc.
10,10.33.6.77,a0:8c:fd:14:7c:c5,Hewlett Packard
22,10.33.6.251,34:56:fe:a0:d2:86,Cisco Meraki
24,10.33.6.1,ac:17:c8:5c:74:ac,Cisco Meraki
2,10.33.6.20,60:c7:98:c9:ec:63,Verifone
3,10.33.6.11,ec:b1:d7:61:b7:f6,Hewlett Packard
4,10.33.6.21,60:c7:98:c9:ec:68,Verifone
23,10.33.6.250,34:56:fe:a0:d3:2d,Cisco Meraki
19,172.28.7.224,00:17:55:e8:37:79,GE Security
22,10.33.6.122,14:20:5e:1f:6f:1d,Apple Inc.
23,172.28.7.212,fc:d8:48:88:00:58,Apple Inc.

如您所见,标头切换端口位于两个CSV中。我想创建一个python脚本,如果csv2 switchport与csv1 switchport匹配,则写入csv1 +附加来自csv2的数据,如果不匹配,则写入csv1 + NO IP,NO MAC,NO VENDOR

有什么想法吗?当我这样在for循环中写入输出时:

if sp == switch_port:
                            vendor = requests.get('https://api.macvendors.com/' + client_item['mac'])
                            vendor = (format(str(vendor.text)))
                            vendor = vendor.replace(",","")
                            time.sleep(1)
                            writer.writerow({csv_headers[0]:name,csv_headers[1]:switch_port,csv_headers[2]:switch_port_name,csv_headers[3]:vlan,csv_headers[4]:ip,csv_headers[5]:mac,csv_headers[6]:vendor})

                        elif sp != switch_port:
                            writer.writerow({csv_headers[0]:name,csv_headers[1]:switch_port,csv_headers[2]:switch_port_name,csv_headers[3]:vlan,csv_headers[4]:'No IP',csv_headers[5]:'No MAC',csv_headers[6]:'No vendor'})

我得到两次输入,但数据没有追加。

我想要这样的输出:

Switch,Switchport Number,Port Name,VLAN,IP Address,Mac Address,Manufactor

谢谢!

1 个答案:

答案 0 :(得分:1)

您正在寻找LEFT JOIN,可以使用pandas轻松实现:

import pandas as pd

# load tables and unify join key for simplicity
df1 = pd.read_csv("csv1.csv")
df2 = pd.read_csv("csv2.csv")
df2.rename(columns={"Switch Port": "Switchport Number"}, inplace=True)

# merge tables
merged_df = pd.merge(df1, df2, on=["Switchport Number"], how="left")

# replace NaN values with default strings
default_values = {"IP Address": "NO IP", "Mac Address": "NO MAC", "Manufactor": "NO VENDOR"}
merged_df.fillna(value=default_values, inplace=True)

# write result to file
merged_df.to_csv("csv3.csv", index=False)