将 csv 文件中的列(字符串)转换为整数元组

问题描述 投票:0回答:0

目前我使用 csv 模块处理一个文件,该模块创建了一个列表 字典。

import csv
file = open('csvfile.csv)
lines = csv.reader(file)
header = next(lines)  # ['name', 'price', 'date']
# when I do the following
for line in lines:
    print(line)
# I get the following     
['xxxx', '5.00', '2/23/2023']

# assigning types to the columns to do type conversion
types = [
    str,
    float,
    str  # this need to be a tuple]
# now to create a list of dicts
alist_of_dicts = [{
    name:func(val)
    for name, func, val in zip(header, types, line)
} for line in lines]
            

如何选择第三列

str(2/23/2023)
以使用我当前使用的格式更改为
tuple(2, 21, 2007)

python csv
© www.soinside.com 2019 - 2024. All rights reserved.