Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write data to an excel file?

Tags:

python

excel

I have some data that I'd like to save in an excel file. How does one do this in python?

like image 469
fruit Avatar asked Dec 04 '22 12:12

fruit


2 Answers

There's a great python module called XLWT. I'd recommend using that... it writes native Excel files instead of CSVs. Supports formulas, etc too.

Documentation (borrowed from Mark)

like image 60
kafuchau Avatar answered Dec 06 '22 00:12

kafuchau


I'll answer a slightly different question: "How can I write data so that Excel can read it?"

Use the csv module to write your data as a .csv file, and then open it in Excel.

import csv
csvout = csv.writer(open("mydata.csv", "wb"))
csvout.writerow(("Country", "Year"))
for coutry, year in my_data_iterable():
    csvout.writerow((country, year))
like image 38
Ned Batchelder Avatar answered Dec 06 '22 01:12

Ned Batchelder