【问题标题】:Export a table in CSV file导出 CSV 文件中的表格
【发布时间】:2015-11-29 15:26:11
【问题描述】:

我以 2 的幂及其以 2 为底的对数通过以下方式生成了一个表格:

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

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

【问题讨论】:

    标签: python csv export-to-csv


    【解决方案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
    

    【讨论】:

    • 按照你的例子,数据只存储在一列中...我需要将数据写入单独的单元格中!
    • 您可以在 python 或 Excel 中更改分隔符,但它们应该相同
    猜你喜欢
    • 1970-01-01
    • 2023-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多