2017-11-01 10 views
0

私は安静のエンドポイントを持っています。私の残りのAPIはそれにリクエストを出し、そのファイルはzipファイルです。このzipファイルには2つのファイルがあります。私はこのzipアーカイブから1つのファイルの内容を読みたいだけです。私はテストをすることができたし、私のコードがline file = zipfile.ZipFile(io.BytesIO(response_object.content))にスタックしているのが好きです。Pythonは、API呼び出しからファイルを読み込みます。

クラスZipFileResponseHandler:

def __init__(self,**args): 
    self.csv_file_to_index = args['csv_file_to_index'] 

def __call__(self, response_object, raw_response_output, response_type, req_args, endpoint): 
    file = zipfile.ZipFile(io.BytesIO(response_object.content)) 
    for name in file.namelist(): 
     if re.match(name, self.csv_file_to_index): 
      data =file.read(name) 
      print_xml_stream(repr(data)) 

答えて

0

だから私は自分自身の答えへの解決​​策を見つけました。私はPython 2.7を使用しているので、response_objectを処理するための対応するメソッドはBytesIOではなくStringIOです。だから、ライン:

ファイル= zipfile.ZipFile(io.BytesIO(response_object.content))

ファイル= zipfile.ZipFile(StringIO.StringIO(response_object.content))である必要があり

関連する問題