简体   繁体   中英

Export a table in CSV file

I have generated a table with the powers of 2 and their logarithm in base 2 in the following way:

import math
x = 2.0
while x < 100.0:
    print x, '\t', math.log(x)/math.log(2)
    x = x + x

How can I export this table in a CSV file with each element that matches exactly one cell?

see https://docs.python.org/2/library/csv.html#csv.writer

import math
import csv

x = 2.0
with open('out.csv', 'wb') as f:
    writer = csv.writer(f, delimiter=',')
    while x < 100.0:
        print x, '\t', math.log(x)/math.log(2)
        writer.writerow([x, math.log(x)/math.log(2)])
        x = x + x

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM