When I read data back in from a CSV file, every cell is interpreted as a string.
I too really liked @martineau's approach and was especially intrigued by his comment that the essence of his code was a clean mapping between fields and types. That suggested to me that a dictionary would work also. Hence the variation on his theme shown below. It's worked nicely for me.
Clearly the value field in the dictionary is really just a callable and thus could be used to provide a hook for data massaging as well as typecasting if one so chose.
import ast
import csv
fix_type = {'IsActive': bool, 'Type': str, 'Price': float, 'States': ast.literal_eval}
filename = 'test_transform.csv'
with open(filename, newline='') as file:
for i, row in enumerate(csv.DictReader(file)):
row = {k: fix_type[k](v) for k, v in row.items()}
print(f'row {i}: {row}')
Output
row 0: {'IsActive': True, 'Type': 'Cellphone', 'Price': 34.0, 'States': [1, 2]}
row 1: {'IsActive': False, 'Type': 'FlatTv', 'Price': 3.5, 'States': [2]}
row 2: {'IsActive': True, 'Type': 'Screen', 'Price': 100.23, 'States': [5, 1]}
row 3: {'IsActive': True, 'Type': 'Notebook', 'Price': 50.0, 'States': [1]}