Reputation: 784
Hello and thank you for taking the time to read this. I want to extract large amounts of text from one database to another. The problem is that when I read the text from the database it contains a lot of "\N" "\n". I would really appreciate it if someone could point me in the right direction or tell what is going on here exactly and why the command fetchall is behaving like this...
Thank you in advance!
Example of the text in the TABLE:ourdata (I want to read from):
Example of text in the TABLE product_d I am writing to:
This is the code I am using:
import sqlite3
database_path = "E:/Thesis stuff/Python/database/"
conn = sqlite3.connect(database_path + 'test2.db')
c = conn.cursor()
current_number = 0
# creates the table to which the descriptions go
c.execute("CREATE TABLE IF NOT EXISTS product_d (product_description TEXT)")
conn.commit()
c.execute("SELECT description FROM ourdata")
text_to_format = c.fetchall()
print(text_to_format[0])
text_list = []
# make a list of the descriptions
for text in text_to_format:
text = str(text)
text_list.append(text)
# put all the elements of the list into the new table
for item in text_list:
c.execute("INSERT INTO product_d (product_description) VALUES (?)", (text_list[current_number],))
print("at number " + str(current_number))
current_number += 1
conn.commit()
c.close()
conn.close()
Upvotes: 0
Views: 26
Reputation: 7840
The giveaway is the parentheses around the string in the second example. fetchall()
returns a list of tuples, and in your "make a list of the descriptions" block, you're explicitly converting those tuples to strings. Instead, what you want to do is simply grab the first (and only, in this case) element of the tuple. It should be as simple as changing this line:
text = str(text)
to this:
text = text[0]
Upvotes: 1