在python

时间:2016-03-16 20:15:04

标签: python csv delimiter writer

我正在尝试使用以下代码在python中的csv文件中写入数据:

with open('test.csv', 'w') as csvfile:
writer = csv.writer(csvfile, delimiter=',')

writer.writerow(['filename', 'pump_wavelength', 'input_pump_power', 'signal_wavelength', 'input_signal_power',
                 'output_idler_wavelength', 'output_idler_power', 'delta_beta', 'gamma'])
for i in z:
    ---------- some code to get values to be written --------------
    writer.writerow([filename, pump_wavelength, input_pump_power, signal_wavelength, input_signal_power,
                     output_idler_wavelength, output_idler_power, delta_beta, gamma])

给出以下输出:

  filename,pump_wavelength,input_pump_power,signal_wavelength,input_signal_power,output_idler_wavelength,output_idler_power,delta_beta,gamma
  5.SPE,1555.503,6.654,1554.507,0.054,1554.9618225,-62.3483855665,-6.55390005475,"(1.4790380500687297, -6.5539000547525852, 0.00015485724131853652)"
  6.SPE,1555.503,6.654,1554.507,0.054,1555.09420064,-62.0169964106,-6.55390005475,"(1.5365574572884448, -6.5539000547525852, 0.00015485724131853652)"

为什么在csv文件中写入的每一行的最后三个值周围都有“(和)”?我正在使用python 3。

2 个答案:

答案 0 :(得分:1)

没关系,因为这个值本身包含逗号,所以它可能被错误地读取。 CSV模块会根据标准自动将其括在引号中。

答案 1 :(得分:0)

您正在文件中写入9个值,其中最后一个是三元组