convert a column(string) in csv file to a tuple of ints
Question:
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?
Answers:
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
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)}]
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?
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
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)}]