【问题标题】:Easiest way to export dictionary (or current output text file) to .csv file instead of a .txt file?将字典(或当前输出文本文件)导出到 .csv 文件而不是 .txt 文件的最简单方法?
【发布时间】:2019-07-18 17:33:15
【问题描述】:

我为日志文件创建了一个解析器和提取器,并希望看到一个快速方法的示例:

  1. 将当前输出写入 .txt 文件并将其转换为新的 .csv 文件(可能带有 pandas),或者

  2. 使用 .csv 模块将写入方法序列更改为csv.writer,然后使用csv.DictReader

就实用性和资源消耗而言,什么是最有效的?我当前导出的.txt文件和相关代码贴在下面。

导出的数据:

Request ID : bf710010
Username   : kadaniel
ECID       : 6ca4862b-14d1-4a7f-8158-5e6cac363144-001477ac
Start Time : 2019-06-12T09:14:54.947
End Time   : 2019-06-12T09:14:55.22

Request ID : bf710020
Username   : kadaniel
ECID       : 6ca4862b-14d1-4a7f-8158-5e6cac363144-001477ac
Start Time : 2019-06-12T09:14:55.343
End Time   : 2019-06-12T09:14:55.514

代码:

process_records = {}

with open(log_file_path, "r") as file:

    for line in file:
        m = pattern.match(line)
        if m is not None:        # If there is a match with pattern
            (timestamp, ecid, requestid, username) = m.groups()
            if requestid not in process_records:
                process_records[requestid] = (timestamp, username, ecid, None)
            else:
                process_records[requestid] = process_records[requestid][:3] + (timestamp,)

    for requestid, (start, username, ecid, end) in process_records.items():
        print("Request ID: {}\nUsername: {}\nECID: {}\nStart Time: {}\nEnd Time: {}\n\n".format(
                requestid,
                username,
                ecid,
                start,
                end,
            ))

file.close()

with open(export_file, 'w+') as file:

    file.write("EXPORTED DATA:\n\n")

    if pattern != None:
        for requestid, (start, username, ecid, end) in process_records.items():
                file.write(("Request ID : {}\nUsername   : {}\nECID       : {}\nStart Time : {}\nEnd Time   : {}\n\n".format(
                    requestid,
                    username,
                    ecid,
                    start,
                    end,
                )))

file.close()

我目前在字典process_records 中有数据。每个键 (requestid) 与元组中的 4 个元素相关联。我希望键和之后的每个元素代表它自己的列。

【问题讨论】:

    标签: python python-3.x csv export-to-csv


    【解决方案1】:

    在我看来,理想的方法是使用内置的 csv 库。

    首先,导入库。

    import csv
    

    然后用下面的sn-p写——

    with open(export_file, 'w+') as file_handler:
        csv_writer = csv.writer(fileobj=file_handler, delimiter=',')
        for requestid, (start, username, ecid, end) in process_records.items():
            csv_writer.writerow([requestid, username, ecid, start, end,])
    

    【讨论】:

    • +1 用于使用现有的 csv 库,可能比我们一起破解的任何东西都测试得更多。您可能需要添加如何包含标题行。
    【解决方案2】:

    CSV 以逗号分隔。你不需要熊猫。只改变写记录的规则

    with open(export_file, 'w+') as file:
    
        file.write("Request ID,Username,ECID,Start Time,End Time\n") # header
    
        if pattern != None:
    
            for requestid, (start, username, ecid, end) in process_records.items():
                    file.write(("{},{},{},{},{}\n".format(
                        requestid,
                        username,
                        ecid,
                        start,
                        end,
                    )))  # record
    
    file.close()
    

    【讨论】:

    • 当其中一个属性中有逗号时会发生什么?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-23
    • 1970-01-01
    • 2015-06-10
    • 2021-06-24
    • 1970-01-01
    相关资源
    最近更新 更多