将数据作为python中的float值写入csv文件

时间:2017-06-09 07:20:03

标签: string python-3.x csv floating-point

我想使用python作为浮点值将数据写入.csv文件。每次我尝试使用csv模块的编写器函数来执行此操作时,它会将其写入csv文件但是在引号中,将其呈现为string。我也无法使用float()函数,因为数据获取位于list。提前致谢

1 个答案:

答案 0 :(得分:0)

我相信这是正确的行为。来自wiki 'Comma-separated values'

  

标准化

     
    

可以引用任何字段(带双引号)。

  

修改

为避免引用,您可以在csv writer中设置quoting=csv.QUOTE_NONE。 Python 3.5.2的工作示例:

import csv
with open('eggs.csv', 'w', newline='') as csvfile:
    spamwriter = csv.writer(csvfile, delimiter=',',
                            quotechar='|', quoting=csv.QUOTE_NONE)
    spamwriter.writerow([str(1.1112233), str(1.3355)])

egg.csv

1.1112233,1.3355