Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Format csv cells as text with python

I am giving a row of data to write to a csv file. They are mostly float type numbers. But when it writes to the csv file, the cells are default in custom format. So if I have an input number like 3.25, it prints as "Mar 25". How can I avoid this?
This is the piece of code:

data = [0.21, 3.25, 25.9, 5.2]  
f = open('Boot.csv','w')  
out = csv.writer(f, delimiter=';', quoting=csv.QUOTE_NONE)  
out.writerow(data)  
like image 927
remy Avatar asked Sep 23 '26 04:09

remy


1 Answers

The csv module is writing the data fine - I'm guessing that you're opening it in Excel to look at the results and that Excel is deciding to autoformat it as a date.

It's an excel issue, you need to tell it not to play around with that field by changing it to Text (or anything that isn't General)

If you're writing Excel data, you may want to look at the xlwt module (check out the very useful site http://www.python-excel.org/) - then your value types will not be so liable to fluctuate.

like image 160
Jon Clements Avatar answered Sep 24 '26 19:09

Jon Clements



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!