TypeError when trying to convert Python 2.7 code to Python 3.4 code
I am having trouble converting the code below, which was written for Python 2.7
, to code that is compatible in Python 3.4
. I am getting error TypeError: can't concat bytes to str
in line outfile.write(decompressedFile.read())
. So I replaced the line with outfile.write(decompressedFile.read().decode("utf-8", errors="ignore"))
, but this resulted in an error with the same error.
import os
import gzip
try:
from StirngIO import StringIO
except ImportError:
from io import StringIO
import pandas as pd
import urllib.request
baseURL = "http://ec.europa.eu/eurostat/estat-navtree-portlet-prod/BulkDownloadListing?file="
filename = "data/irt_euryld_d.tsv.gz"
outFilePath = filename.split('/')[1][:-3]
response = urllib.request.urlopen(baseURL + filename)
compressedFile = StringIO()
compressedFile.write(response.read().decode("utf-8", errors="ignore"))
compressedFile.seek(0)
decompressedFile = gzip.GzipFile(fileobj=compressedFile, mode='rb')
with open(outFilePath, 'w') as outfile:
outfile.write(decompressedFile.read()) #Error
source to share
The problem is that a GzipFile
byte oriented object needs to be wrapped, but you are passing StringIO
which is textual. Use instead io.BytesIO
:
from io import BytesIO # Works even in 2.x
# snip
response = urllib.request.urlopen(baseURL + filename)
compressedFile = BytesIO() # change this
compressedFile.write(response.read()) # and this
compressedFile.seek(0)
decompressedFile = gzip.GzipFile(fileobj=compressedFile, mode='rb')
with open(outFilePath, 'w') as outfile:
outfile.write(decompressedFile.read().decode("utf-8", errors="ignore"))
# change this too
source to share