Добрый день, написал код, который позволяет собирать данные с конфига XML и записывает эти данные в CSV - Таблицу. Не получается разобраться, почему у меня все данные записываются в один столбец. Решение есть, что в Excel я сделал "Разделить по столбцам" ориентир запятая и данные записались верно. Но хотелось бы узнать, где проблема в коде.
import xmltodict
import csv
with open("XML-FILE", "r") as cfg:
dict = xmltodict.parse(cfg.read())
num = 1
with open('ACL.csv', 'w', newline="") as acl_file:
fieldnames = ["ID","Order","Enabled", "Name", "Description", "IpVersion", "Src", "Dst", "Proxy", "Service", "ValidTime", "Action", "NAT", "SNAT", "DNAT"]
listitem = dict['config']['list']['listitem']
writer = csv.DictWriter(acl_file, fieldnames=fieldnames, delimiter=',')
writer.writeheader()
for i in listitem:
Dubl_name_ID = False
Dubl_name_Order = False
Dubl_name_Enabled = False
Dubl_name_Name = False
Dubl_name_Description = False
Dubl_name_IpVersion = False
Dubl_name_Src = False
Dubl_name_Dst = False
Dubl_name_Proxy = False
Dubl_name_Service = False
Dubl_name_Validtime = False
Dubl_name_Action = False
Dubl_name_NAT = False
Dubl_name_SNAT = False
Dubl_name_DNAT = False
line = {}
for j in i['variable']:
try: text = j["#text"]
except KeyError: text = "None"
if j["@name"] == "Id":
if Dubl_name_ID is False:
line["ID"] = text
elif Dubl_name_ID is True:
line["ID"] = line["ID"] + "*" + text
Dubl_name_ID is True
if j["@name"] == "Order":
if Dubl_name_Order is False:
line["Order"] = text
elif Dubl_name_Order is True:
line["Order"] = line["Order"] + "*" + text
Dubl_name_Order = True
if j["@name"] == "Enabled":
if Dubl_name_Enabled is False:
line["Enabled"] = text
elif Dubl_name_Enabled is True:
line["Enabled"] = line["Enabled"] + "*" + text
Dubl_name_Enabled = True
if j["@name"] == "Name":
if Dubl_name_Name is False:
line["Name"] = text
elif Dubl_name_Name is True:
line["Name"] = line["Name"] + "*" + text
Dubl_name_Name = True
if j["@name"] == "Description":
if Dubl_name_Description is False:
line["Description"] = text
elif Dubl_name_Description is True:
line["Description"] = line["Description"] + "*" + text
Dubl_name_Description = True
if j["@name"] == "IpVersion" :
if Dubl_name_IpVersion is False:
line["IpVersion"] = text
elif Dubl_name_IpVersion is True:
line["IpVersion"] = line["IpVersion"] + "*" + text
Dubl_name_IpVersion = True
if j["@name"] == "Src":
if Dubl_name_Src is False:
line['Src'] = text
elif Dubl_name_Src is True:
line['Src'] = line['Src'] + "*" + text
Dubl_name_Src = True
if j["@name"] == "Dst" :
if Dubl_name_Dst is False:
line['Dst'] = text
elif Dubl_name_Dst is True:
line['Dst'] = line['Dst'] + "*" + text
Dubl_name_Dst = True
if j["@name"] == "Proxy":
if Dubl_name_Proxy is False:
line['Proxy'] = text
elif Dubl_name_Proxy is True:
line['Proxy'] = line['Proxy'] + "*" + text
Dubl_name_Proxy = True
if j["@name"] == "Service" :
if Dubl_name_Service is False:
line['Service'] = text
elif Dubl_name_Service is True:
line['Service'] = line['Service'] + "*" + text
Dubl_name_Service = True
if j["@name"] == "ValidTime" :
if Dubl_name_Validtime is False:
line['ValidTime'] = text
elif Dubl_name_Validtime is True:
line['ValidTime'] = line['ValidTime'] + text
Dubl_name_Validtime = True
if j["@name"] == "Action" :
if Dubl_name_Action is False:
line['Action'] = text
elif Dubl_name_Action is True:
line['Action'] = line['Action'] + "*" + text
Dubl_name_Action = True
if j["@name"] == "NAT":
if Dubl_name_NAT is False:
line['NAT'] = text
elif Dubl_name_NAT is True:
line['NAT'] = line['NAT'] + "*" + text
Dubl_name_NAT = True
if j["@name"] == "SNAT" :
if Dubl_name_SNAT is False:
line['SNAT'] = text
elif Dubl_name_SNAT is True:
line['SNAT'] = line['SNAT'] + "*" + text
Dubl_name_SNAT = True
if j["@name"] == "DNAT" :
if Dubl_name_DNAT is False:
line['DNAT'] = text
elif Dubl_name_DNAT is True:
line['DNAT'] = line['DNAT'] + "*" + text
Dubl_name_DNAT = True
num+=1
writer.writerow(line)
acl_file.close()
cfg.close()
P.S. Я не умею хорошо писать коды. Если покажите, как бы стоило более читабельно его написать, будет замечательно.