write float list to csv file

The writer.writerow() takes a sequence (a list or tuple), but you are passing in a string instead. By passing in a string, writer.writerow() still treats it as as sequence, and each individual character becomes a column value:

1,.,1,3,,,0,.,2,5,,,3,.,2,8

Moreover, the method converts column to strings for you, don't do this yourself.

Instead build a list of your float values and pass that in:

row = []
for result in result_list:
    row.append(result.get_value())

with open(output_path, "wb") as file:
    writer = csv.writer(file)
    writer.writerow(row)

or even:

with open(output_path, "wb") as file:
    writer = csv.writer(file)
    writer.writerow([r.get_value() for r in result_list])

The delimiter defaults to ,.


Use delimiter and just write the list you don't need to convert it to string:

import csv

float_list = [1.13, 0.25, 3.28]

with open(output_path, "wb") as file:
    writer = csv.writer(file, delimiter=',')
    writer.writerow(float_list)

Output:

1.13,0.25,3.28

Also there is no need to close the file since you used with

Edit:

@Martijn Pieters: delimiter , is default.

From Docs:

A one-character string used to separate fields. It defaults to ','.

This line writer = csv.writer(file, delimiter=',') can be writer = csv.writer(file)

Tags:

Python

Csv