<jupyter_start><jupyter_text>Extracting Titanic Disaster Data From Kaggle **Key Changes**- New Kaggle API to download the data from kaggle. Follow the notebook for the steps.  Setup Kaggle API tokenSteps:- Signup and login to https://kaggle.com - Go to the Account Tab ( `https://www.kaggle.com//account` ) and under API section, click `Create API Token`.  A file named `kaggle.json` will be downloaded.- place `kaggle.json` inside the root level `titanic` folder.- if you are using any code versioning system like git. then make sure `kaggle.json` is an entry in the `.gitignore` file so that you don't accidently share the credentials- install `kaggle` package as shown belowFor more info visit kaggle Github page: https://github.com/Kaggle/kaggle-api<jupyter_code>!pip install --user --upgrade kaggle
# restart terminal
from IPython.display import display_html
def restartkernel() :
    display_html("<script>Jupyter.notebook.kernel.restart()</script>",raw=True)
restartkernel()<jupyter_output><empty_output><jupyter_text>Read API key<jupyter_code>import json
import os

api_file_path = os.path.join(os.path.pardir,'kaggle.json')
with open(api_file_path) as f:
    kaggle_token = json.load(f)
    # kaggle authentication
    os.environ["KAGGLE_USERNAME"] = kaggle_token['username']
    os.environ["KAGGLE_KEY"] = kaggle_token['key']
print(os.environ["KAGGLE_USERNAME"])<jupyter_output>archaean<jupyter_text>Using Kaggle API<jupyter_code>from kaggle.api.kaggle_api_extended import KaggleApi<jupyter_output><empty_output><jupyter_text>Important Step :You need to agree to terms and conditions of the competition for which you want to download the dataset. - This is a one time activity per account. - for Titanic competition : visit https://www.kaggle.com/c/titanic and click on "Join Competition" - and click on "I Understand and Accept"- once done, proceed to next steps<jupyter_code># create kaggle API object
api = KaggleApi()
# authenticate
api.authenticate()
# file paths
raw_data_path = os.path.join(os.path.pardir,'data','raw')
# this will download 'titanic.zip' in the raw_data_path location
api.competition_download_file(competition='titanic', file_name='train.csv',path=raw_data_path, force=True)
api.competition_download_file(competition='titanic', file_name='test.csv' ,path=raw_data_path, force=True)

train_data_path = os.path.join(raw_data_path, 'train.csv')
# printing top 5 rows
!head -5 $train_data_path
!ls ../data/raw<jupyter_output>'ls' is not recognized as an internal or external command,
operable program or batch file.<jupyter_text>Builiding the file script<jupyter_code>get_raw_data_script_file = os.path.join(os.path.pardir,'src','data','get_raw_data.py')
%%writefile $get_raw_data_script_file
# -*- coding: utf-8 -*-
import json
import os
import logging

# getting root directory
project_dir = os.path.join(os.path.dirname(__file__), os.pardir, os.pardir)
# read kaggle API token and create enviornment variables
api_file_path = os.path.join(project_dir,'kaggle.json')
with open(api_file_path) as f:
    kaggle_token = json.load(f)
    # environment variable for kaggle authentication
    os.environ["KAGGLE_USERNAME"] = kaggle_token['username']
    os.environ["KAGGLE_KEY"] = kaggle_token['key']


from kaggle.api.kaggle_api_extended import KaggleApi

def main(project_dir):
    '''
    main method
    '''
    # get logger
    logger = logging.getLogger(__name__)
    logger.info('getting raw data')
    
    # file name : check from the competition data page
    train_file_name = 'train.csv'
    test_file_name = 'test.csv'

    # file paths
    raw_data_path = os.path.join(project_dir,'data','raw')
    
    # extract data
    api = KaggleApi()
    api.authenticate()
    api.competition_download_file(competition='titanic', file_name=train_file_name, path=raw_data_path, force=True)
    api.competition_download_file(competition='titanic', file_name=test_file_name, path=raw_data_path, force=True)
    logger.info('downloaded raw training and test data')


if __name__ == '__main__':
    
    # setup logger
    log_fmt = '%(asctime)s - %(name)s - %(levelname)s - %(message)s'
    logging.basicConfig(level=logging.INFO, format=log_fmt)

    # call the main
    main(project_dir)

!python $get_raw_data_script_file<jupyter_output>Downloading train.csv to ..\src\data\..\..\data\raw

Downloading test.csv to ..\src\data\..\..\data\raw
{'fraction_non_alphanumeric': 0.11258581235697941, 'fraction_numerical': 0.0006864988558352403, 'mean_word_length': 5.079276773296245, 'pattern_counts': {'":': 0, '<': 20, '<?xml version=': 0, '>': 20, 'https://': 4, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 11, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '409554', 'license': 'MIT', 'n_tokens_mistral': 1499, 'n_tokens_neox': 1466, 'n_words': 367, 'path': 'notebooks/1.0-ak-extract-titanic-data.ipynb', 'repo_name': 'donaldheppner/titanic', 'size': '4370'}