dump csv from sqlalchemy

前端 未结 8 455
时光取名叫无心
时光取名叫无心 2020-12-02 17:48

For some reason, I want to dump a table from a database (sqlite3) in the form of a csv file. I\'m using a python script with elixir (based on sqlalchemy) to modify the datab

8条回答
  •  醉梦人生
    2020-12-02 18:10

    import csv
    
    f = open('ratings.csv', 'w')
    out = csv.writer(f)
    out.writerow(['id', 'user_id', 'movie_id', 'rating'])
    
    for item in db.query.all():
        out.writerow([item.username, item.username, item.movie_name, item.rating])
    f.close()
    

提交回复
热议问题