Python csv.writer抑制空格

时间:2018-11-22 22:28:31

标签: python csv

import csv    
with open('bilaterale.csv', 'w', newline='') as csvfile:
        spamwriter = csv.writer(csvfile,delimiter=' ',quotechar=' ',quoting=csv.QUOTE_MINIMAL)
        spamwriter.writerow("0.0,1.0,0.992,0.984,0.976")

如何抑制行中数字之间的空格?

2 个答案:

答案 0 :(得分:2)

只需写:  spamwriter.writerow([0.0,1.0,0.992,0.984,0.976])

而不是:spamwriter.writerow("0.0,1.0,0.992,0.984,0.976")

因为writerow需要列表而不是字符串。

希望这会有所帮助。

答案 1 :(得分:0)

您可以直接在文件对象中写入逗号分隔的字符串,而无需csv模块:

with open('bilaterale.csv', 'w') as csvfile:
    csvfile.writelines("0.0,1.0,0.992,0.984,0.976")