我遇到問題,將代碼編寫爲Python 2.7
,代碼與Python 3.4
兼容。我收到outfile.write(decompressedFile.read())
行中的錯誤TypeError: can't concat bytes to str
。所以我用outfile.write(decompressedFile.read().decode("utf-8", errors="ignore"))
取代了這一行,但是這導致了錯誤同樣的錯誤。嘗試將Python 2.7代碼轉換爲Python代碼時出現TypeError 3.4代碼
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
不寫入模式已成爲''wb''? – TigerhawkT3