简体   繁体   中英

Why only the first dictionary get written in the csv file?

I use IMDbPY library to get for example 100 movies info and store them in the csv file. The problem is only the first movie information in the loop would be written in the csv file.

here is the code :

from imdb import IMDb
import csv
from datetime import datetime

startTime = datetime.now()

ia = IMDb()
movie_id = 2250912

for i in range(1, 101):
    movie = ia.get_movie(movie_id)
    movie_id += i

    print(datetime.now() - startTime)
    print (movie_id)
    print ('\n')

    with open('dict.csv', 'w') as csv_file:
        writer = csv.writer(csv_file)

        for key, value in movie.items():
            writer.writerow([key, value])

Your with statement overwrites the csv every iteration of the while loop. Use 'a' instead of 'w' so the information gets appended.

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