如何使用聊天机器人的 API 将 sqlite3 转换为 csv 格式?

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

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

与 Chatterbot 关联的 db 文件中有多个表。它们是 conversation_association、对话、响应、声明、tag_association 和标签。
在所有这些表中,只有响应表和语句表具有正确的数据(至少在我的情况下)。但是我试图将所有表格转换为 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)