i want to rearange a csv file :
from
SWA hostname IP MAC
SWA0 app1,app2,app3,app4,…etc ip1,ip2,ip3,ip4,,,,etc mac1,mac2,mac3,mac4,…etc
SWA1 app12,app13,app14,..etc ip12,ip13,ip14..etc mac12,mac13,mac14,…etc
SWA2 app18,app19,app20,..etc ip18,ip19,ip20..etc mac18,mac19,mac20,…etc
to
hostname IP MAC SWA
app1 ip1 mac1 SW0
app2 ip2 mac2 SW0
app3 ip3 mac3 SW0
app4 ip4 mac4 SW0
app11 ip11 mac11 SW1
app12 ip12 mac12 SW1
app13 ip13 mac13 SW1
app14 ip14 mac14 SW1
app18 ip18 mac18 SW2
app19 ip19 mac19 SW2
app20 ip20 mac20 SW2
i do generate this csv file by searching in a another csv file using:
def search(csv_file,word):
return [i for i in open(csv_file,'r') if i.find(word) != -1]
and
if s:
resu = search('result/result.csv',search_word)
str(resu)
#print(resu)
lenght = len(resu)
filenam = 'result/result_' + str(datetime.datetime.now()) + '.csv'
with open(filenam,'w') as export:
writer = csv.writer(export,delimiter=";")
writer.writerow(headerList)
for line in resu:
line = line.replace('[','').replace(']','').replace("'",'')
export.write('{}'.format(line))
i want the result diffrently as showen above.
Thanks.
got a partiel answer :
import pandas as pd
df = pd.read_csv('/path/to/sample.csv')
df_reorder = df[['A', 'B', 'C', 'D', 'E']] # rearrange column here
df_reorder.to_csv('/path/to/sample_reorder.csv', index=False)
and now the result is :
hostname ip mac SWA
app1,app2,app3 ip1,ip2,ip3 mac1,mac2,mac3 SW0
app4,app5,app6 ip4,ip5,ip6 mac4,mac5,mac6 SW1
app7,app8,app9 ip7,ip8,ip9 mac7,mac8,mac9 SW2
i need to dispatch them on every line