如何使用Chatbot的API将sqlite3转换为csv格式?

时间:2018-07-13 06:45:52

标签: python-3.x sqlite export-to-csv chatbot

当我运行聊天机器人时,它将在后端创建一个db.sqlite3文件来存储所有对话。我想使用API​​将此db.sqlite3文件转换为csv文件。我应该如何在python中实现它?该图像包含文件的类型。

enter image description here

1 个答案:

答案 0 :(得分:0)

与Chatterbot关联的db文件中有多个表。它们是conversation_association,对话,响应,声明,tag_association和tag。
在所有这些表中,只有响应表和语句表具有适当的数据(至少在我的情况下)。但是我试图将所有表转换为csv。因此,您可能还会找到一些空的csv文件。

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)