简体   繁体   English

将 CSV 文件转换为具有精确类型和顺序的字典列表

[英]Convert CSV file into list of dictionaries with precise type and order

Everything is in the title, I've found this code that almost does what I want: https://stackoverflow.com/a/21572244/5455842一切都在标题中,我发现这段代码几乎可以满足我的要求: https://stackoverflow.com/a/21572244/5455842

import csv

with open('test.csv') as f:
    a = [{k: int(v) for k, v in row.items()}
        for row in csv.DictReader(f, skipinitialspace=True)]

My CSV is made of 5 columns and 6 lines (the first one being fieldnames).我的 CSV 由 5 列和 6 行组成(第一个是字段名)。

My problems are the following:我的问题如下:

  • the third column of my csv are date that needs to be datetime.date object我的 csv 的第三列是需要 datetime.date object 的日期
  • the fourth column of my csv are multiple integer that needs to be a dict of int我的 csv 的第四列是多个 integer 需要是 int 的字典
  • Let's say my fives columns are "1","2","3","4","5": I need the code to make a list of dict in a precise order that is: 3,1,2,5,4假设我的五列是“1”、“2”、“3”、“4”、“5”:我需要代码以精确的顺序制作字典列表:3、1、2、5 ,4

    Here's some sample data:以下是一些示例数据:
 Firstname,Lastname,Birthdate,Answers(good/middle/bad),Comments Mark,Tolonen,12/10/1986,"3154,0",The first one John,Travolta,02/18/1954,"42,21",Would grease again Albert,Einstein,03/14/1879,"18,19,20",This guy is not stupid Isaac,Newton,12/25/1642,"2000,20,20", Should eat apple Alan,Turing,06/23/1912,"42,42,42",Hey what's up

And here's a sample of desired thing:这是所需的示例:

[{'Birthdate': datetime.date(1986, 12, 10),
  'Comments': 'The first one',
  'Firstname': 'Mark',
  'Lastname': 'Tolonen',
  'Answers(good/middle/bad)': [3154, 0]},
 {'Birthdate': datetime.date(1954, 02, 18),
  'Comments': 'Would grease again',
  'Firstname': 'John',
  'Lastname': 'Travolta',
  'Answers(good/middle/bad)': [42, 21]},
...
}]

Given your sample data and desired output, if you use Python 3.7 or later, the dictionary order will be as desired:给定您的示例数据和所需的 output,如果您使用 Python 3.7 或更高版本,则字典顺序将根据需要:

import csv
from datetime import datetime
from pprint import pprint

order = 'Birthdate','Comments','Firstname','Lastname','Answers(good/middle/bad)'

with open('input.csv') as f:
    reader = csv.DictReader(f)
    data = []

    for row in reader:

        # Post-process the non-string columns.
        row['Birthdate'] = datetime.strptime(row['Birthdate'],'%m/%d/%Y').date()
        row['Answers(good/middle/bad)'] = [int(x) for x in row['Answers(good/middle/bad)'].split(',')]

        # Re-write the dict with the desired key order.
        # Python 3.7 (or CPython 3.6) or later required to keep insertion order.
        # 3.7 made insertion order preservation part of the language spec.
        # Specifically, the implementation of CPython 3.6 preserves insertion order
        # as an implementation detail.
        # For older versions use collections.OrderedDict instead.
        data.append({k:row[k] for k in order})
        
pprint(data)

Output: Output:

[{'Birthdate': datetime.date(1986, 12, 10),
  'Comments': 'The first one',
  'Firstname': 'Mark',
  'Lastname': 'Tolonen',
  'Answers(good/middle/bad)': [3154, 0]},
 {'Birthdate': datetime.date(1954, 2, 18),
  'Comments': 'Would grease again',
  'Firstname': 'John',
  'Lastname': 'Travolta',
  'Answers(good/middle/bad)': [42, 21]},
 {'Birthdate': datetime.date(1879, 3, 14),
  'Comments': 'This guy is not stupid',
  'Firstname': 'Albert',
  'Lastname': 'Einstein',
  'Answers(good/middle/bad)': [18, 19, 20]},
 {'Birthdate': datetime.date(1642, 12, 25),
  'Comments': ' Should eat apple',
  'Firstname': 'Isaac',
  'Lastname': 'Newton',
  'Answers(good/middle/bad)': [2000, 20, 20]},
 {'Birthdate': datetime.date(1912, 6, 23),
  'Comments': "Hey what's up",
  'Firstname': 'Alan',
  'Lastname': 'Turing',
  'Answers(good/middle/bad)': [42, 42, 42]}]

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

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