timbram
timbram

Reputation: 1865

Python 3 - printing rows in csv reader object results in a single character on each line

I am trying to migrate some code from Python 2 to Python 3 and cannot figure out why it is printing one character at a time as if it is reading the file as one long string.

I have been looking into it and maybe a need to use newline='' when opening the file?

But how can I do that when using urlopen()?

import csv
import urllib.request
url = "http://samplecsvs.s3.amazonaws.com/Sacramentorealestatetransactions.csv"
ftpstream = urllib.request.urlopen(url)

csvfile = ftpstream.read().decode('utf-8')
csvfile = csv.reader(csvfile, delimiter=',')

for row in csvfile:
    print(row)

Upvotes: 0

Views: 450

Answers (1)

Andy
Andy

Reputation: 161

Try to change

csvfile = ftpstream.read().decode('utf-8')

to

csvfile = ftpstream.read().decode('utf-8').split('\r')

Upvotes: 1

Related Questions