Home > Mobile >  How to remove brackets when bringing values from sqlite database?
How to remove brackets when bringing values from sqlite database?

Time:03-11

I am new to Python. I wanna know how to get rid of brackets when bringing values from sqlite. This is what I have:

con = sqlite3.connect(database=r'ims.db')
cur = con.cursor()
wb = load_workbook('example.xlsx')
ws = wb.active
cur.execute("Select color1, color2, color3, color4, color5, color6, color7 from colors")
row= cur.fetchall()
row_num = 14
for element in row:
    ws[f'C{row_num}'].value = str(element)
    row_num  = 1

So for now in the excel file, it shows('black', 'yellow', 'pink', '', '', '', '') and('red', 'blue', '', '', '', '', '') but is there anyway to make it to black,yellow,pink...... So that I could insert them into different columns in excel. Anyone help tqsm!

CodePudding user response:

I would probably use pandas to help you with this

import sqlite3
import pandas 

con = sqlite3.connect(database=r'ims.db')
cur = con.cursor()
wb = load_workbook('example.xlsx')
ws = wb.active
query = "Select color1, color2, color3, color4, color5, color6, color7 from colors"
df = pd.read_sql(query, con)

This will put it into a dataframe from which you can do a lot more stuff

  • Related