Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to remove brackets when bringing values from sqlite database?

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!

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading