How to convert sqlite3 to csv format using API for a chatbot?

心已入冬 提交于 2019-12-13 03:56:50

问题


When I run my chatbot it creates a db.sqlite3 file in backend for storing all the conversation. I want to convert this db.sqlite3 file into a csv file using API. How should I implement it in python? The image contains the type of file.


回答1:


There are multiple tables in the db file associated with Chatterbot. They are conversation_association, conversation, response, statement, tag_association and tag.
Out of all these tables only response and statement tables have proper data (at least in my case). However I tried to convert all tables into csv. So you may find some empty csv files too.

import sqlite3, csv
db = sqlite3.connect("chatterbot-database")  # enter your db name here
cursor = db.cursor()
tables = [table[0] for table in cursor.execute("select name from sqlite_master where type = 'table'")]  # fetch table names from db

for table in tables:
    with open('%s.csv'%table, 'w') as fd:   
        csvwriter = csv.writer(fd)
        for data in cursor.execute("select * from '%s'"%table):  # get data from each table
            csvwriter.writerow(data)


来源:https://stackoverflow.com/questions/51319143/how-to-convert-sqlite3-to-csv-format-using-api-for-a-chatbot

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!