使用 Python 将纯文本文件解析为 CSV

Parsing plain text file into CSV with Python

我一直在关注这个主题的回复:Python: parsing structured text to CSV format 但我在导出数据时卡住了。我对原始主题中最佳投票结果提出的脚本进行了一些小修改,但我只能将最后一组结果放入 CSV 文件中,而没有写入任何其他内容。

原文为:

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

加了小mod的代码是:

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)

我真的卡住了,我不知道为什么脚本只写最后一组数据。拜托,有人可以帮助我吗?

提前致谢。

您用来分隔记录的条件不太正确,试试这个:

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