Learn, Share, Build

247
September 19, 2017, at 2:36 PM

I have a zip file train.zip(1.1GB) which I wanted to import into a Python Notebook, unzip and then set out to work on it. I imported it as a String IO object utilizing the option Inert StringIO Object.

from io import StringIO
import requests
import json
import pandas as pd
# @hidden_cell
# This function accesses a file in your Object Storage. The definition contains your credentials.
# You might want to remove those credentials before you share your notebook.
def get_object_storage_file_with_credentials_xxxxxx(container, filename):
    """This functions returns a StringIO object containing
    the file content from Bluemix Object Storage."""
    url1 = ''.join(['https://identity.open.softlayer.com', '/v3/auth/tokens'])
    data = {'auth': {'identity': {'methods': ['password'],
            'password': {'user': {'name': 'member_xxxxxx','domain': {'id': 'xxxxxxx'},
            'password': 'xxxxx),(xxxxx'}}}}}
    headers1 = {'Content-Type': 'application/json'}
    resp1 = requests.post(url=url1, data=json.dumps(data), headers=headers1)
    resp1_body = resp1.json()
    for e1 in resp1_body['token']['catalog']:
        if(e1['type']=='object-store'):
            for e2 in e1['endpoints']:
                        if(e2['interface']=='public'and e2['region']=='dallas'):
                            url2 = ''.join([e2['url'],'/', container, '/', filename])
    s_subject_token = resp1.headers['x-subject-token']
    headers2 = {'X-Auth-Token': s_subject_token, 'accept': 'application/json'}
    resp2 = requests.get(url=url2, headers=headers2)
    return StringIO(resp2.text)
# Your data file was loaded into a StringIO object and you can process the data.
# Please read the documentation of pandas to learn more about your possibilities to load your data.
# pandas documentation: http://pandas.pydata.org/pandas-docs/stable/io.html
data_1 = get_object_storage_file_with_credentials_20e75635ab104e58bd1a6e91635fed51('DefaultProjectxxxxxxxx', 'train.zip')

This gives an output:

data_1
<_io.StringIO at 0x7f8a288cd3a8>

But when I try to use Zipfile to unzip it, I'm greeted with the following error:

from zipfile import ZipFile
file = ZipFile(data_1)
BadZipFile: File is not a zip file

How to I access the file in IBM DSX?

Answer 1

The ZipFile constructor expects a filename, not file content. See here for a solution: Unzip buffer with Python?

READ ALSO
Learn, Share, Build

Learn, Share, Build

This is my code:

174
Learn, Share, Build

Learn, Share, Build

i just create a env by conda, and intall packages follow readmewhen i do flask run, the questions occured

236
Learn, Share, Build

Learn, Share, Build

I am new to Django and so I thought of creating basic app that represents operations in a shipping companyI have a WorkOrder which contains Shipments

230