I'm trying to update some code. Am learning Python3 at the moment and wondering if this could be done better with a dictionary or tuple perhaps? The code looks at a csv file & prints the values out. I'm trying to update the code to make it simpler and more robust (if possible).
use a sample piece of data as:
brand,model,year,colour,engine_size,top_speed,transmission
bmw,X6,2018,black,4L,240,manual
# use a sample piece of data as:
# brand,model,year,colour,engine_size,top_speed,transmission
# bmw,X6,2018,black,4L,240,manual
import csv
brand, model, year, colour, engine_size, top_speed, transmission = {},
{}, {}, {}, {}, {}, {}
counter = 0
with open("csv_test.csv", 'r') as csvFile:
reader = csv.DictReader(csvFile)
for row in reader:
brand[counter] = row['brand']
model[counter] = row['model']
year[counter] = row['year']
colour[counter] = row['colour']
engine_size[counter] = row['engine_size']
top_speed[counter] = row['top_speed']
transmission[counter] = row['transmission']
counter += 1
csvFile.close()
I know DictReader will use the top row as column name, I want to go through each row & print the values to the screen.