Skip to main content
 首页 » 编程设计

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

2025年05月04日14itcoder

我正在开发一个函数,该函数接受 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 )。