导出CSV文件

时间:2015-11-29 15:26:11

标签: python csv export-to-csv

我已经通过以下方式生成了一个功能为2的表及其在对数2中的对数:

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

如何将此表格导出为CSV文件,其中每个元素只与一个单元格匹配?

1 个答案:

答案 0 :(得分:1)

请参阅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
相关问题