import os import re import sqlite3 from bs4 import BeautifulSoup # open connection to database conn = sqlite3.connect('notes-backup.db') cursor = conn.cursor() # execute query to fetch note data cursor.execute('SELECT title, body FROM notes') # save each note as a file for title, body in cursor: # Remove any special characters that could cause problems in a Unix filename safe_title = re.sub(r'[^\w_.)( -]', '', title) filename = 'data/{}.txt'.format(safe_title) with open(filename, 'w') as f: Text = BeautifulSoup(body, 'html.parser').get_text() PlainText = Text.encode('mac-roman').decode("utf-8") f.write(PlainText) # close the database connection conn.close()