gpt4 book ai didi

python - CSV 到字典列表 - 更好的方法?

转载 作者:太空宇宙 更新时间:2023-11-04 07:36:16 24 4
gpt4 key购买 nike

我正在开发一个函数,该函数接受 CSV 文件名并将每一行转换为字典,然后返回创建的字典列表(以便能够在以后的函数中迭代和组织。我已经通过执行以下操作让它按照我的意愿进行操作,但我觉得必须有更好的方法。有什么改进建议吗?

import re

def import_incidents(filename):
"""Imports CSV and returns list of dictionaries for each incident"""
with open(filename, 'r') as file:
data = file.read()
data = data.split('\n')
list_of_data = []
headers = True
for line in data:
line = line.split('","')
if headers == True:
#Skip header and set to false
headers = False
elif len(line) == 1 or line[3] == '':
#File always has a 1 lenth final line, skip it.
#Events can leave blank policies, skip those too.
pass
else:
temp_dict = {}
temp_dict['id'] = re.sub('"', '', line[0])
temp_dict['time'] = re.sub('GMT-0600','',line[1])
temp_dict['source'] = line[2]
temp_dict['policy'] = line[3]
temp_dict['destination'] = line[5]
temp_dict['status'] = line[10]
list_of_data.append(temp_dict)

return list_of_data

print(import_incidents('Incidents (Yesterday Only).csv'))

CSV 内容示例:

"ID","Incident Time","Source","Policies","Channel","Destination","Severity","Action","Maximum Matches","Transaction Size","Status",
"9511564","29 Dec. 2015, 08:33:59 AM GMT-0600","Doe, John","Encrypted files","HTTPS","blah.blah.com","Medium","Permitted","0","47.7 KB","Closed - Authorized",
"1848446","29 Dec. 2015, 08:23:36 AM GMT-0600","Smith, Joe","","HTTP","google.com","Low","Permitted","0","775 B","Closed"

最佳答案

您重新发明了 csv.DictReader() class ,恐怕:

import csv

def import_incidents(filename):
with open(filename, 'r', newline='') as file:
reader = csv.DictReader(file)
for row in reader:
if not row or not row['Policies']:
continue
row['Incident Time'] = re.sub('GMT-0600', '', row['Incident Time'])
yield row

这依赖于字典键的标题行。您可以使用 DictReader()fieldnames 参数定义您自己的字典键(fieldnames 字段按顺序与文件),但文件中的第一行仍然像其他任何行一样被读取。您可以使用 next() 函数跳过行(参见 Skip the headers when editing a csv file using Python )。

关于python - CSV 到字典列表 - 更好的方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34537335/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com