pythoncsvplaintext

Parsing plain text file into CSV with Python


I was following the responses from this topic: Python: parsing structured text to CSV format and I'm stuck when I'm exporting the data. I made some little modifications to the script proposed by the best voted result in the original topic, but I'm only able to get the last set of results into the CSV file and nothing else is written.

The original text is:

Device ID:PE2-CONCE
SysName:null
Entry address(es):nullnullnullnull 
IPv4 address:172.31.232.42
Platform:Cisco 7204VXR Capabilities  Router 
Interface:GigabitEthernet0/0/0/14
Port ID (outgoing port):GigabitEthernet0/3
Holdtime:168 sec
Device ID:PE2-CORE-TEMUCO.entel.cl
SysName:nullPE2-CORE-TEMUCO.entel.cl
Entry address(es):nullnullnullnull 
IPv4 address:200.72.146.226
Platform:cisco ASR9K Series Capabilities  Router 
Interface:TenGigE0/10/0/3
Port ID (outgoing port):TenGigE0/10/0/3
Holdtime:171 sec
Device ID:PE2-PCS-RANCAGUA
SysName:null
Entry address(es):nullnullnullnull 
IPv4 address:192.168.204.153
Platform:cisco CISCO7606 Capabilities  Router Switch IGMP 
Interface:TenGigE0/5/0/1
Port ID (outgoing port):TenGigabitEthernet4/2
Holdtime:163 sec
Device ID:PE1-RECREO
SysName:nullPE1-RECREO
Entry address(es):nullnullnullnull 
IPv4 address:200.72.146.103
Platform:cisco ASR9K Series Capabilities  Router 
Interface:TenGigE0/0/0/0
Port ID (outgoing port):TenGigE0/0/1/0
Holdtime:153 sec

And the code with little mods is:

def read_records(iterable):
    record = {}
    for line in iterable:
        if line.isalnum():
            # Nuevo registro, mantiene anterior
            if record:
                yield record
            record = {}
            continue
        key, value = line.strip().split(":",1)
        record[key.strip()] = value.strip()

    # Archivo listo
    if record:
        yield record


# Salida encabezados
headers = ("Device ID", "SysName", "Entry address(es)", "IPv4 address", "Platform", "Interface", "Port ID (outgoing port)", "Holdtime")

with open("inputFile.txt") as infile, open("outputFile.csv", 'wb') as outfile:
    records = read_records(infile)
    writer = csv.DictWriter(outfile, headers, delimiter=';')
    writer.writeheader()

    # and write
    writer.writerows(records)

I'm really stuck and I don't know why the script only writes the last set of data only. Please, can somebody help me please?

Thanks in advance.


Solution

  • The condition you use to separate records is not quite right, try this:

    def read_records(iterable):
        record = {}
        for line in iterable:
            if line.startswith('Device'):
                if record:
                    yield record
                record = {}
            key, value = line.strip().split(":",1)
            record[key.strip()] = value.strip()
        if record:
            yield record