I'm trying to import a CSV file data and define the first value of each line as a variable to a class object or a dict. I managed to create dictionaries from imported data, but can access them individually. Any enlightenment? Thank you.
CSV type:
name,gender,age,country,location
John,M,53,Brazil,São Paulo
import csv
file = "info.csv"
csv_open = open(file, newline="")
csv_data = csv.reader(csv_open)
data = []
for line in csv_data: # All CSV data as a list
data.append(line)
class Person:
"""Simple register with name, gender, age, country and
location info."""
def __init__(self, name, gender, age, country, location):
self.name = name
self.gender = gender
self.age = age
self.country = country
self.location = location
idx = 0
for elem in data:
*problem_here_no_var* = Person( data[idx][0],
data[idx][1],
data[idx][2],
data[idx][3],
data[idx][4])
idx += 1
print(John.country)
#My intention is to access a specific object and return any of it's attributes.