You can't create
'James': {'Game_1':'3'}, {'Game_2':'7'}, {'Game_3':'4'}
(if you don't mean tuple of dicts)
It has to be:
list of dicts
'James': [ {'Game_1':'3'}, {'Game_2':'7'}, {'Game_3':'4'} ]
tuple of dicts
'James': ( {'Game_1':'3'}, {'Game_2':'7'}, {'Game_3':'4'} )
or one dict
'James': {'Game_1':'3', 'Game_2':'7', 'Game_3':'4'}
You use csv.DictReader() so every row is already dict and you should use row['name'] instead of row[0]
You should first create empty list score_sheet[row['name']] = [] and later use for-loop to append() dictionares with {game: row[game]}
for row in reader:
score_sheet['name'] = []
for game in list_of_games:
score_sheet[row[0]].append( {game:row[game]} )
And it should give
score_sheet = {
'James': [ {'Game_1':'3'}, {'Game_2':'7'}, {'Game_3':'4'} ],
# ...
}
You may also use list/tuple comprehension in place of normal for-loop.
# list comprehension
score_sheet[row['name']] = [{game:row[game]} for game in list_of_games]
# tuple comprehension
score_sheet[row['name']] = tuple({game:row[game]} for game in list_of_games)
If you want it as one dict
score_sheet = {
'James': {'Game_1':'3', 'Game_2':'7', 'Game_3':'4'},
# ...
}
then first you have to create empty dict score_sheet[row['name']] = {} and later use for-loop to move games to this dictionary score_sheet[row['name']][game] = row[game]
for row in reader:
score_sheet[row['name']] = {}
#print(row)
for game in list_of_games:
score_sheet[row['name']][game] = row[game]
You may also use dict comprehension
# dict comprehension
score_sheet[row['name']] = {game:row[game] for game in list_of_games}
Full working code used for tests.
I used io.StringIO only to emulate file-like object in memory - so everyonce can it copy and run.
#
text = '''name,Game_1, Game_2, Game_3
James,3,7,4
Charles,2,3,8
Bob,6,2,4
'''
import csv
import io
# --- version with list/tuple ---
score_sheet = {}
with io.StringIO(text) as scores_file:
#with open("scores.txt") as scores_file:
reader = csv.DictReader(scores_file)
list_of_games = reader.fieldnames[1:]
for row in reader:
#score_sheet[row['name']] = []
#print(row)
#for game in list_of_games:
# score_sheet[row['name']].append( {game:row[game]} )
# if you need `tuple of dicts` instead of `list of dicts`
#score_sheet[row['name']] = tuple(score_sheet[row['name']])
# ---
# list comprehension
score_sheet[row['name']] = [{game:row[game]} for game in list_of_games]
# tuple comprehension
score_sheet[row['name']] = tuple({game:row[game]} for game in list_of_games)
print(score_sheet)
# --- version with dict ---
score_sheet = {}
with io.StringIO(text) as scores_file:
#with open("scores.txt") as scores_file:
reader = csv.DictReader(scores_file)
list_of_games = reader.fieldnames[1:]
for row in reader:
#score_sheet[row['name']] = {}
#print(row)
#for game in list_of_games:
# score_sheet[row['name']][game] = row[game]
# ---
# dict comprehension
score_sheet[row['name']] = {game:row[game] for game in list_of_games}
print(score_sheet)
EDIT:
Because csv.DictReader() gives dictonary in every row (as @KelluBundy also mentioned in comment) so you can also remove name from row to get dictionary only with games and it doesn't need to create new dict for games. And it doesn't need list_of_games
with io.StringIO(text) as scores_file:
#with open("scores.txt") as scores_file:
reader = csv.DictReader(scores_file)
#list_of_games = reader.fieldnames[1:] # no need it
for row in reader:
name = row.pop('name') # get `name` and remove it from `row`
score_sheet[name] = row
print()(andprint(type(...)),print(len(...)), etc.) to see which part of code is executed and what you really have in variables. It is called"print debugging"and it helps to see what code is really doing.