您可以使用 pandas 或标准库来执行此操作。 Pandas 通常更快更容易阅读。
设置:
from textwrap import dedent
def write_file(name, string):
with open(name, 'w') as f:
f.write(dedent(string).lstrip())
write_file('File1.csv', """
switch1,Gi1/0/22,connected,716,a-full,a-100,10/100/1000BaseTX
switch2,Fa3/0/8,connected,716,a-full,a-100,10/100BaseTX
switch3,Fa2/0/5,connected,716,a-full,a-100,10/100BaseTX
""")
write_file('File2.csv', """
switch1,716,0040.0020.0010,DYNAMIC,Gi1/0/22
switch2,716,0030.0020.1010,DYNAMIC,Fa3/0/8
switch3,716,0050.0030.1010,DYNAMIC,Fa2/0/5
""")
write_file('File3.csv', """
switch1,Gi1/0/22,0,32,0,33,0,9
switch2,Fa3/0/8,0,0,0,0,0,362
switch3,Fa2/0/5,0,10,20,0,0,100
""")
熊猫代码:
import pandas as pd
t1 = pd.read_csv('File1.csv', names=['switch_name', 'interface', 'col3', 'col4', 'col5', 'col6', 'col7'])
t2 = pd.read_csv('File2.csv', names=['switch_name', 'col2', 'col3', 'col4', 'interface'])
t3 = pd.read_csv('File3.csv', names=['switch_name', 'interface', 'col3', 'col4', 'col5', 'col6', 'col7', 'col8'])
result = t2[['switch_name', 'interface', 'col3']].merge(t3, on=['switch_name', 'interface'])
result.to_csv('Final.csv', header=False, index=False)
with open('Final.csv') as f:
print f.read()
# switch1,Gi1/0/22,0040.0020.0010,0,32,0,33,0,9
# switch2,Fa3/0/8,0030.0020.1010,0,0,0,0,0,362
# switch3,Fa2/0/5,0050.0030.1010,0,10,20,0,0,100
标准库代码:
import csv
# store data in a dictionary for later reference
with open('File3.csv') as f:
f3_data = {(r[0], r[1]): r[2:8] for r in csv.reader(f)}
with open('File2.csv') as f2, open('Final.csv', 'w') as f:
final = csv.writer(f)
for switch_name, col2, col3, col4, interface in csv.reader(f2):
if (switch_name, interface) in f3_data:
final.writerow([switch_name, interface, col3] + f3_data[switch_name, interface])
with open('Final.csv') as f:
print f.read()
# switch1,Gi1/0/22,0040.0020.0010,0,32,0,33,0,9
# switch2,Fa3/0/8,0030.0020.1010,0,0,0,0,0,362
# switch3,Fa2/0/5,0050.0030.1010,0,10,20,0,0,100