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

huangapple go评论54阅读模式
英文:

convert a column(string) in csv file to a tuple of ints

问题

要选择第三列,将字符串“2/23/2023”更改为元组(2, 23, 2023),您可以在类型列表中将第三列的类型更改为一个函数,该函数将执行所需的转换。然后,您可以在创建字典的列表时应用此新的函数。以下是您的代码的修改部分:

import csv

file = open('csvfile.csv')
lines = csv.reader(file)
header = next(lines)

# 修改第三列的类型为一个函数,该函数将执行转换
types = [
    str,
    float,
    lambda date_str: tuple(map(int, date_str.split('/')))
]

# 创建包含字典的列表
alist_of_dicts = [
    {
        name: func(val)
        for name, func, val in zip(header, types, line)
    }
    for line in lines
]

这将使第三列的字符串“2/23/2023”转换为元组(2, 23, 2023)。不过请注意,要确保文件中的日期字符串的格式始终是“M/D/YYYY”,以便分割和转换工作正常。

英文:

Currently, I process a file using the csv module, which creates a list of dictionaries.

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 using a function
types = [
    str,
    float,
    str  # this need to be a tuple
    # tried tuple(map(int, cannotchoosecolumn.split('/')))
    # did not work
]

# 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
]

How would I select the third column str(2/23/2023) to change to a tuple(2, 21, 2007) using the format I am currently using?

答案1

得分: 2

你可以将一个函数传递给你的 types 列表:

import datetime

def read_date(s):
    d = datetime.datetime.strptime(s, "%m/%d/%Y")
    return (d.month, d.day, d.year)

header = ["name", "price", "date"]
lines = [["xxxx", "5.00", "2/23/2023"]]
types = [
    str,
    float,
    read_date,
]

alist_of_dicts = [
    {name: func(val) for name, func, val in zip(header, types, line)} for line in lines
]

print(alist_of_dicts)
# 输出: [{'name': 'xxxx', 'price': 5.0, 'date': (2, 23, 2023)}]

这段代码可能有点难以理解。相反,我建议你使用 csv.DictReader 来将 CSV 文件读取为字符串 -> 字符串的字典,然后进行列的转换。

英文:

You can pass a function to your types list:

import datetime


def read_date(s):
    d = datetime.datetime.strptime(s, "%m/%d/%Y")
    return (d.month, d.day, d.year)


header = ["name", "price", "date"]
lines = [["xxxx", "5.00", "2/23/2023"]]
types = [
    str,
    float,
    read_date,
]

alist_of_dicts = [
    {name: func(val) for name, func, val in zip(header, types, line)} for line in lines
]

print(alist_of_dicts)
# prints: [{'name': 'xxxx', 'price': 5.0, 'date': (2, 23, 2023)}]

This is hard to understand code though. Instead, I recommend you use csv.DictReader to read the csv as a dictionary of strings -> strings, then transforming the columns

答案2

得分: 2

使用csv.DictReader 并在读取时转换列:

import csv

with open('csvfile.csv', newline='') as file:
    a_list_of_dicts = []
    for line in csv.DictReader(file):
        line['price'] = float(line['price'])
        line['date'] = tuple(int(n) for n in line['date'].split('/'))
        a_list_of_dicts.append(line)

print(a_list_of_dicts)

csvfile.csv

name,price,date
xxxx,5.00,2/23/2023
yyyy,6.75,2/24/2023

输出:

[{'name': 'xxxx', 'price': 5.0, 'date': (2, 23, 2023)}, {'name': 'yyyy', 'price': 6.75, 'date': (2, 24, 2023)}]
英文:

Use a csv.DictReader and convert the columns as you read them:

import csv

with open('csvfile.csv', newline='') as file:
    a_list_of_dicts = []
    for line in csv.DictReader(file):
        line['price'] = float(line['price'])
        line['date'] = tuple(int(n) for n in line['date'].split('/'))
        a_list_of_dicts.append(line)

print(a_list_of_dicts)

csvfile.csv

name,price,date
xxxx,5.00,2/23/2023
yyyy,6.75,2/24/2023

Output:

[{'name': 'xxxx', 'price': 5.0, 'date': (2, 23, 2023)}, {'name': 'yyyy', 'price': 6.75, 'date': (2, 24, 2023)}]

huangapple
  • 本文由 发表于 2023年2月24日 06:49:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/75551108.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定