Reputation: 37
I am trying to download a .csv file and save it onto my computer. However when I run the script below, I am getting the error "Error: line contains NULL byte". What is it that I am doing wrong?
import csv
import urllib2
url = 'http://wildfire.alberta.ca/reports/activedd.csv'
response = urllib2.urlopen(url)
cr = csv.reader(response)
for row in cr:
print row
Upvotes: 2
Views: 4423
Reputation: 2944
The file you're trying to download is in UTF-16 format, and the CSV module is not designed for that. You need to decode it from UTF-16 into some other format. For example:
import csv
import codecs
import urllib2
url = 'http://wildfire.alberta.ca/reports/activedd.csv'
response = urllib2.urlopen(url)
cr = csv.reader([x.strip() for x in codecs.iterdecode(response, 'UTF-16')])
data = [x for x in cr]
# Manipulate the data here
# Now to save the CSV:
with open('outputfile.csv', 'wb') as output:
writer = csv.writer(output)
writer.writerows(data)
If you just need to download the file, and not manipulate it, there are better ways (see minitoto's answer).
This is an example, and the newlines have to be stripped manually for this to work properly, so I'm sure there's probably a better way, but that's the main issue
Upvotes: 3
Reputation: 4837
I guess the easiest way is to use urlretrieve
:
import urllib
url = 'http://wildfire.alberta.ca/reports/activedd.csv'
urllib.urlretrieve(url, "activedd.csv")
Upvotes: 1
Reputation: 711
Here's what I've done. The lazy way.
import urllib2
url = 'http://wildfire.alberta.ca/reports/activedd.csv'
response = urllib2.urlopen(url)
with open('activeddData.csv','w') as csvFile:
for line in response.readlines():
csvFile.write(line)
Upvotes: -1