如何使用Chatbot的API将sqlite3转换为csv格式? [英] How to convert sqlite3 to csv format using API for a chatbot?
问题描述
当我运行聊天机器人时,它将在后端创建一个db.sqlite3文件来存储所有对话。我想使用API将此db.sqlite3文件转换为csv文件。我应该如何在python中实现它?图像包含文件的类型。
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.
推荐答案
与Chatterbot关联的db文件中有多个表。它们是conversation_association,对话,响应,声明,tag_association和tag。
在所有这些表中,只有响应表和语句表具有适当的数据(至少在我的情况下)。但是我试图将所有表转换为csv。因此,您可能还会发现一些空的csv文件。
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)
这篇关于如何使用Chatbot的API将sqlite3转换为csv格式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!