3

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
1
  • Doesn't the write mode have to be 'wb'? Commented Jun 23, 2015 at 0:46

1 Answer 1

3

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
Sign up to request clarification or add additional context in comments.

2 Comments

Thank You. But I now get the Error TypeError: 'str' does not support the buffer interface in the line compressedFile.write(response.read().decode("utf-8", errors="ignore"))
@user131983: Fixed, see edit. You could probably just do compressedFile=BytesIO(response.read())...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.