Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python csv.writer control precision

i'm using the standard csv.writer like so

wr = csv.writer(f,csv.QUOTE_NONNUMERIC)
wr.writerows(output)

and i would like to be able to format the csv file so that every column has %3.4e. output is a 2 dimensional list. right now, it seems to be writing it out in whatever precisions the values were stored in, which can be variable. i also don't know at code time the size of each dimension, specifically, how many elements are in each row, so i can't simply write out a big format string. is there a simple way to use the csv.writer to just write every numeric value with a certain precision?

like image 483
tychodin Avatar asked Mar 19 '26 20:03

tychodin


1 Answers

You can still output on arbitrary length list with format strings, though you probably want to use csv.QUOTE_NONE:

wr = csv.writer(f, quoting=csv.QUOTE_NONE)
for i in output:
    wr.writerow(['{:3.4e}'.format(x) for x in i])
like image 159
AChampion Avatar answered Mar 22 '26 09:03

AChampion