Hi, I am trying to write a list with the key, "score" to a CSV file. Ultimately, I would like to store the scores along three other attributes (initials, gender and class) and be able to display it as a Dataframe.
print("Game over! Your score was: ", score)
leaderboard_dict[score] = [current_player_initials, current_player_gender, real_player_class]
print(leaderboard_dict)
with open('leaderboard_doc.csv', 'w') as f:
w = csv.DictWriter(f, leaderboard_dict.keys())
w.writeheader()
w.writerow(leaderboard_dict)
pd.read_csv(path)
This is my current code, but when I run it, I get an error saying, "Error Tokenizing: Expected 1 field in line 12, got 2"
Does anyone know a solution to this? I have been stuck in my code for a while trying to figure this one out. It is almost as if it is not recognizing the formatting when going into the CSV file.
f = open('leaderboard_doc.csv', 'w')
f.write("score,initials,gender,class")
for score in leaderboard_dict:
initials,gender,class = leaderboard_dict[score]
f.write("%s,%s,%s,%s" % (score,initials,gender,class))
f.close()
Note, however, that your dictionary itself is a bit flawed in that you only allow a single instance of every score. I'm not sure what exactly the rest of the context is, but I would recommend having the keys be something that are guaranteed to be unique, eg initials in this case
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.