我正在生成3个CSV,我希望将其组合成一个。我只需要每个文件中的某些列,但我需要它们来匹配开关号和接口
File1中
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
文件2
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
文件3
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
我正试图让最终的csv看起来像这样:
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
是File3的交换机名称,接口,第3列,以及File3
中的第3-8列如果你不想提供答案,不要寻找确切的答案,而是更多的总体思路/方向。对python来说还是个新手。
答案 0 :(得分:1)
您可以使用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
答案 1 :(得分:0)
您可以先一次打开3个文件,用csv
库将它们读入嵌套的行列表中,然后提取您需要的列并将它们写入文件:
from csv import reader
# open all files at once
with open('file1.csv') as f1, \
open('file2.csv') as f2, \
open('file3.csv') as f3:
# convert them to reader objects
csv_files = reader(f1), reader(f2), reader(f3)
# open file to write to
with open('combined.csv', 'w') as out:
# go over each row from the files at once using zip()
for row1, row2, row3 in zip(*csv_files):
# extract columns into a list
line = row1[:2] +[row2[2]] + row3[3:]
# write to the file
out.write(','.join(line) +'\n')
# print contents of new file
print(open('combined.csv').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