尝试将Python 2.7代码转换为Python 3.4代码时出现TypeError

时间:2015-06-23 00:42:48

标签: python

我遇到了将下面代码Python 2.7转换为Python 3.4兼容代码的问题。我在行TypeError: can't concat bytes to str中收到错误outfile.write(decompressedFile.read())。所以我用outfile.write(decompressedFile.read().decode("utf-8", errors="ignore"))替换了这一行,但这导致了错误相同的错误。

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 个答案:

答案 0 :(得分:3)

问题是GzipFile需要包装面向字节的文件对象,但是你传递的是StringIO,它是面向文本的。请改用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