【发布时间】:2019-07-18 17:33:15
【问题描述】:
我为日志文件创建了一个解析器和提取器,并希望看到一个快速方法的示例:
将当前输出写入 .txt 文件并将其转换为新的 .csv 文件(可能带有
pandas),或者使用 .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