0

I'm trying to create a .csv file using python. The .csv file is successfully created however it skips a row between entries giving the result below.

['Name', 'Profession']
[]
['Derek', 'Software Developer']
[]
['Steve', 'Software Developer']
[]
['Paul', 'Manager']
[]

Any help in fixing this would be greatly appreciated.

import csv

with open('persons.csv', 'w') as csvfile:
    filewriter = csv.writer(csvfile, delimiter=',',
                            quotechar='|', quoting=csv.QUOTE_MINIMAL)
    filewriter.writerow(['Name', 'Profession'])
    filewriter.writerow(['Derek', 'Software Developer'])
    filewriter.writerow(['Steve', 'Software Developer'])
    filewriter.writerow(['Paul', 'Manager'])


with open('persons.csv', 'rt') as f:
    reader = csv.reader(f)

    # read file row by row
    for row in reader:
        print(row)
Ayrton Bourn
  • 365
  • 5
  • 16

1 Answers1

0

I was mistakenly using python 2 syntax rather than pythonn 3, I should have had newline='' as a parameter in with open('persons.csv', 'w') as csvfile: to give with open('persons.csv', 'w', newline='') as csvfile:

Ayrton Bourn
  • 365
  • 5
  • 16