在Python 3中,我有一个程序从链接(zipurl)中的zip中提取文件:
from io import BytesIO
from urllib.request import urlopen
from zipfile import ZipFile
zipurl = (f'http://agencia.tse.jus.br/estatistica/sead/odsele/prestacao_contas/prestacao_final_2014.zip')
with urlopen(zipurl) as zipresp:
with ZipFile(BytesIO(zipresp.read())) as zfile:
zfile.extractall('doacoes_2014')
请问,有没有办法只解压缩一个文件?如果我需要文件" receitas_candidatos_2014_brasil.txt"
答案 0 :(得分:1)
您可以使用ZipFile.extract()提取单个文件而不是.extractall():
zfile.extract('receitas_candidatos_2014_brasil.txt', 'doacoes_2014')