简体   繁体   中英

Python - Exported data to CSV in loop only saves last line of output

I have a dictionary of game names within the variable gameList. From there I can successfully print each game name, but when attempting to save each game in the list into a CSV, it will only save the last game name that is outputted, not the entire list.

for i in gameList:
    #print(games[i]['name'])
    csvGames = games[i]['name']
    with open('exportedGamesList.csv', 'w', encoding='utf-8') as csvfile:
        for line in csvGames:
            csvfile.write(line)

Any assistance would be appreciated :)

您应该将with open('exportedGamesList.csv', 'a', encoding='utf-8') as csvfile:附加到文件中with open('exportedGamesList.csv', 'a', encoding='utf-8') as csvfile:

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM