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

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

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

问题

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

  1. import csv
  2. file = open('csvfile.csv')
  3. lines = csv.reader(file)
  4. header = next(lines)
  5. # 修改第三列的类型为一个函数,该函数将执行转换
  6. types = [
  7. str,
  8. float,
  9. lambda date_str: tuple(map(int, date_str.split('/')))
  10. ]
  11. # 创建包含字典的列表
  12. alist_of_dicts = [
  13. {
  14. name: func(val)
  15. for name, func, val in zip(header, types, line)
  16. }
  17. for line in lines
  18. ]

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

英文:

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

  1. import csv
  2. file = open('csvfile.csv')
  3. lines = csv.reader(file)
  4. header = next(lines) # ['name', 'price', 'date']
  5. # when I do the following
  6. for line in lines:
  7. print(line)
  8. # I get the following
  9. ['xxxx', '5.00', '2/23/2023']
  10. # assigning types to the columns to do type conversion using a function
  11. types = [
  12. str,
  13. float,
  14. str # this need to be a tuple
  15. # tried tuple(map(int, cannotchoosecolumn.split('/')))
  16. # did not work
  17. ]
  18. # now to create a list of dicts
  19. alist_of_dicts = [
  20. {
  21. name: func(val)
  22. for name, func, val in zip(header, types, line)
  23. }
  24. for line in lines
  25. ]

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 列表:

  1. import datetime
  2. def read_date(s):
  3. d = datetime.datetime.strptime(s, "%m/%d/%Y")
  4. return (d.month, d.day, d.year)
  5. header = ["name", "price", "date"]
  6. lines = [["xxxx", "5.00", "2/23/2023"]]
  7. types = [
  8. str,
  9. float,
  10. read_date,
  11. ]
  12. alist_of_dicts = [
  13. {name: func(val) for name, func, val in zip(header, types, line)} for line in lines
  14. ]
  15. print(alist_of_dicts)
  16. # 输出: [{'name': 'xxxx', 'price': 5.0, 'date': (2, 23, 2023)}]

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

英文:

You can pass a function to your types list:

  1. import datetime
  2. def read_date(s):
  3. d = datetime.datetime.strptime(s, "%m/%d/%Y")
  4. return (d.month, d.day, d.year)
  5. header = ["name", "price", "date"]
  6. lines = [["xxxx", "5.00", "2/23/2023"]]
  7. types = [
  8. str,
  9. float,
  10. read_date,
  11. ]
  12. alist_of_dicts = [
  13. {name: func(val) for name, func, val in zip(header, types, line)} for line in lines
  14. ]
  15. print(alist_of_dicts)
  16. # 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 并在读取时转换列:

  1. import csv
  2. with open('csvfile.csv', newline='') as file:
  3. a_list_of_dicts = []
  4. for line in csv.DictReader(file):
  5. line['price'] = float(line['price'])
  6. line['date'] = tuple(int(n) for n in line['date'].split('/'))
  7. a_list_of_dicts.append(line)
  8. print(a_list_of_dicts)

csvfile.csv

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

输出:

  1. [{'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:

  1. import csv
  2. with open('csvfile.csv', newline='') as file:
  3. a_list_of_dicts = []
  4. for line in csv.DictReader(file):
  5. line['price'] = float(line['price'])
  6. line['date'] = tuple(int(n) for n in line['date'].split('/'))
  7. a_list_of_dicts.append(line)
  8. print(a_list_of_dicts)

csvfile.csv

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

Output:

  1. [{'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:

确定