A
A
Aibot922021-06-10 12:59:08
Python
Aibot92, 2021-06-10 12:59:08

Python how to save in excel?

Friends help
, there is a function for saving from sql to excel
and I can’t figure out how to save not the last result, but the entire search result,
I even understand that in theory I need to save it as a sheet, but I can’t figure out how to break it down by values

def finde_sql(find):
    cursor, connect = sql_tab()
    find_a = '%' + '%%'.join(find.split()) + '%'
    for values in cursor.execute(f"SELECT id,brand,model,description,prise,old_prise,date FROM svz_parsing WHERE model LIKE '{find_a}' "):
        id, brand, model, description, prise, old_prise, date = values
        data_svz = pd.DataFrame({'Бренд':[brand],'Модель':[model],'Описание':[description],'цена':[prise],'старя цена':[old_prise],'дата обновления':[date]})
        data_svz.to_excel(direct+'/data_pars.xlsx', sheet_name='Связной', index=False)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander, 2021-06-10
@shabelski89

here at each iteration you overwrite DF

data_svz = pd.DataFrame({'Бренд':[brand],'Модель':[model],'Описание':[description],'цена':[prise],'старя цена':[old_prise],'дата обновления':[date]})

but you need to define DF before the loop, and in the loop add each line
id, brand, model, description, prise, old_prise, date = values
data = {'Бренд':[brand],'Модель':[model],'Описание':[description],'цена':[prise],'старя цена':[old_prise],'дата обновления':[date]}
data_svz = data_svz.append(data , ignore_index=True)

and after exiting the loop, already save to excel

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question