0

I have the following problem. I have a for loop, in which i want to add always the newcoming element of a list to a new line in a csv file. But something is wrong with my code. If i open the file, there is alwys a space between the rows and the list brackets are also existing.

My code extract:

    allParameterCombi.append([fRC_Network1.jc,fRC_Network1.radius,fRC_Network1.frcSize, fRC_Network1.percent[0],fRC_Network1.percent[1],fRC_Network1.distType, fRC_Network1.ID])

    with open('parameterCombinatons.csv','a') as csv_file:
        writer = csv.writer(csv_file, delimiter=";")
        writer.writerow([allParameterCombi[count3-1]])
    count3 = count3 +1

How it looks like opened: enter image description here

Varlor
  • 1,161
  • 2
  • 12
  • 34

1 Answers1

1

You have to open your file in binary mode ("ab") before passing them to csv.writer like this:

with open('parameterCombinatons.csv','ab') as csv_file:  #python3 open('parameterCombinatons.csv','a',newline='')
Tiny.D
  • 6,026
  • 2
  • 11
  • 18
  • Thank you, it works! What is the difference between a and ab? – Varlor Jun 27 '17 at 12:55
  • @Varlor `b` means open file in _binary_ format in python2. – Tiny.D Jun 27 '17 at 12:57
  • refer to this answer (https://stackoverflow.com/questions/1170214/python-2-csv-writer-produces-wrong-line-terminator-on-windows/1170297#1170297) for more details. – Tiny.D Jun 27 '17 at 13:01