简体   繁体   English

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

[英]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.我正在关注这个主题的回复: Python:将结构化文本解析为 CSV 格式,我在导出数据时卡住了。 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.我对原始主题中最佳投票结果提出的脚本进行了一些小修改,但我只能将最后一组结果放入 CSV 文件中,而没有写入任何其他内容。

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.提前致谢。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM