V
V
Vadim kyklaed2017-02-20 22:57:57
Python
Vadim kyklaed, 2017-02-20 22:57:57

How to write from db to csv?

Good evening.
Please tell me, I have a tuple obtained from the database consisting of 4 elements for example ("LLC horns and hooves", "# 123", "12.20.2016", "12.20.2017")
I need to write uX in csv so that each the element of the tuple was in its cell. How to do it ?

with open('list_to_csv.csv', 'w') as csv_file:
    csv_writer = csv.writer(csv_file)
    for num,item in enumerate(a):
        csv_writer.writerow(a[num])

So it turns out that I just write each tuple in one cell, and also a break line between each entry turns out
I tried to write it as a string, etc.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
N
nirvimel, 2017-02-21
@kyklaed

You probably need a different CSV dialect . Try playing with the dialect parameter in csv.writer , try csv.excel, csv.excel_tab, csv.unix_dialect values, or text strings like "excel". The format of the output CSV will then change. Try until you get separate cells.
If it does not help, then you will have to manually set the delimiter and quotechar parameters.

D
Dmitry Zaitsev, 2017-02-21
@3a9cdmitr

Of course, I just recently started learning the language, so maybe I didn’t understand something, but for writing to csv, it’s easier csv_file.write(';'.join(a))

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question