8

I have a list of float numbers, its format is:

float_list = [1.13, 0.25, 3.28, ....]

I used following code to write it to a .csv file:

float_str = ""

for result in result_list:
    float_str = float_str + str(result.get_value()) + ","

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

file.close()

where result.get_value() is:

def get_value():
    return float(value)

I expect to see that in each cell, there is a value. However, what I've got looks like:

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

It seems that each of the digits occupies a cell.

So, may I know how can I solve this issue and get what I expected?

4 Answers 4

7

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 ,.

Sign up to request clarification or add additional context in comments.

2 Comments

You say you don't need to do the conversion, but leave in the example any? I think the whole row thing here is redundant... writer.writerow(result_list) (going by OP's get_value())
@JonClements: Then at the very least writer.writer([r.get_value() for r in result_list]) is required.
6

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)

1 Comment

delimiter=',' is the default.
4

I just want to write a number, like 1234, into csv file (delimiter=' '), it ends up like 1 2 3 4. I got the following answer from another site but would like to share it here:

number = 1.13
writer.writerow([number])

Comments

1

The below code writes a list of float numbers into a csv file.

import csv

float_str=['1.500','2.48933','7.36945']

with open('output_path','w',newline='') as file:

    write=csv.writer(file, delimiter='\n')
    for num in float_str:
        write.writerow(num)

2 Comments

This is just wrong, at least try out your code before you post it. Also it doesn't add anything to the other answers.
It adds delimiter = \n which is best for exporting data into column format

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.