R.Bahl
R.Bahl

Reputation: 409

Python: writing int and list in a csv row

Maybe this question is too naive, but is giving me a hard time!. I want to write 2 float values and a list of int to a row in csv file in a loop. The file may or may not exist before an attempt is made to write in it. In case it does not, a new file should be created. This is what I am doing:

f = open('stat.csv','a')
try:
    writer=csv.writer(f,delimiter=' ',quoting=csv.QUOTE_MINIMAL)
    writer.writerow((some_float1,some_float2,alist))
finally:
    f.close()

where alist = [2,3,4,5]. I am getting the following output:

 some_float1 some_float2 "[2,3,4,5]"

What I want is this:

 some_float1 some_float2 2 3 4 5

i.e. I would like to get rid of the "", the square brackets and make the delimiter consistent throughout. Any suggestions ?

Upvotes: 0

Views: 6885

Answers (1)

SingleNegationElimination
SingleNegationElimination

Reputation: 156148

How about:

writer.writerow([some_float1, some_float2] + alist)

Upvotes: 5

Related Questions