M
M
Maloyyy2021-10-24 13:15:31
Python
Maloyyy, 2021-10-24 13:15:31

In Python How to write data to multiple CSV columns?

Good afternoon, I wrote a code that allows you to collect data from the XML config and writes this data to a CSV - Table. I can't figure out why I have all the data written in one column. The solution is that in Excel I did the "Split by columns" landmark with a comma and the data was written correctly. But I would like to know where the problem is in the code.

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()


PS I'm not good at writing codes. If you show how it would be worth writing it more readably, it will be great.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
dmshar, 2021-10-24
@Maloyyy

What "column" are you talking about? CSV is a regular TEXT file, in which all data is simply "by agreement" divided by a separator. No more.
Therefore, if you simply open this file in MS EXCEL, it is quite obvious that each record will be represented by one line that falls into one - usually the first - column of the table.
If you want to work with such a file in the future just like with a table, then already in EXCEL you need to import their csv data into xslx format. This one is done like this:
Tab "Data" -> Get external data ->From text->specify the name of your csv-file ->choose the format "with delimiter"->Next->select the delimiter used (for example "comma")->Next->Finish- >specify the place on the sheet from which the imported table should begin (usually "=$A$1")->OK
and see what you get in your now real EXCEL table.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question