Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeError when trying to convert Python 2.7 code to Python 3.4 code

Tags:

python

I am having issues converting the code below which was written for Python 2.7 to code compatible in Python 3.4. I get the error TypeError: can't concat bytes to str in the line outfile.write(decompressedFile.read()). So I replaced the line with outfile.write(decompressedFile.read().decode("utf-8", errors="ignore")), but this resulted in the error 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
like image 344
user131983 Avatar asked Jun 08 '26 20:06

user131983


1 Answers

The problem is that GzipFile needs to wrap a bytes-oriented file object, but you're passing a StringIO, which is text-oriented. Use io.BytesIO instead:

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
like image 142
Kevin Avatar answered Jun 11 '26 09:06

Kevin