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
user131983
  • 3,787
  • 4
  • 27
  • 42

1 Answers1

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
Kevin
  • 28,963
  • 9
  • 62
  • 81
  • 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 Jun 23 '15 at 01:03
  • 1
    @user131983: Fixed, see edit. You could probably just do `compressedFile=BytesIO(response.read())`... – Kevin Jun 23 '15 at 01:04