{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "" ] }, { "cell_type": "markdown", "source": "# Data Wrangler I/O Operations\n\nThis tutorial covers the I/O capabilities of data-wrangler, including loading and saving data from various sources and formats.\n\n## Overview\n\nThe `datawrangler.io` module provides seamless loading and saving of data from:\n\n- **Local files**: CSV, JSON, text files, images, and more\n- **URLs**: Load data directly from web sources\n- **Multiple formats**: Automatic format detection based on file extensions\n- **Mixed sources**: Handle lists of files/URLs with different formats\n\nLet's explore these capabilities with practical examples.", "metadata": {} }, { "cell_type": "code", "source": "import datawrangler as dw\nfrom datawrangler.io import load, save\nimport pandas as pd\nimport numpy as np\nimport os\nfrom pathlib import Path", "metadata": {}, "outputs": [], "execution_count": null }, { "cell_type": "markdown", "source": "## Loading Different File Formats\n\nData-wrangler automatically detects file formats and loads them appropriately. Let's demonstrate with different file types:", "metadata": {} }, { "cell_type": "code", "source": "# Create sample data files for demonstration\nimport tempfile\n\n# Create a temporary directory for our examples\ntemp_dir = tempfile.mkdtemp()\nprint(f\\\"Working in temporary directory: {temp_dir}\\\"\n\n# Create sample CSV file\ncsv_data = pd.DataFrame({\n 'product': ['laptop', 'mouse', 'keyboard', 'monitor'],\n 'price': [999.99, 25.50, 75.00, 300.00],\n 'category': ['electronics', 'accessories', 'accessories', 'electronics']\n})\n\ncsv_file = os.path.join(temp_dir, 'products.csv')\ncsv_data.to_csv(csv_file, index=False)\n\n# Create sample text file\ntext_content = \\\"\\\"\\\"Data science is transforming industries worldwide.\nMachine learning enables computers to learn from data.\nNatural language processing helps computers understand human language.\nData visualization makes complex data insights accessible.\\\"\\\"\\\"\n\ntext_file = os.path.join(temp_dir, 'sample_text.txt')\nwith open(text_file, 'w') as f:\n f.write(text_content)\n\n# Create sample JSON file\njson_data = {\n 'users': [\n {'name': 'Alice', 'age': 30, 'city': 'New York'},\n {'name': 'Bob', 'age': 25, 'city': 'San Francisco'},\n {'name': 'Charlie', 'age': 35, 'city': 'Chicago'}\n ]\n}\n\njson_file = os.path.join(temp_dir, 'users.json')\nimport json\nwith open(json_file, 'w') as f:\n json.dump(json_data, f)\n\nprint(f\\\"Created files:\\\"\nprint(f\\\"- CSV: {csv_file}\\\"\nprint(f\\\"- Text: {text_file}\\\"\nprint(f\\\"- JSON: {json_file}\\\")", "metadata": {}, "outputs": [], "execution_count": null } ], "metadata": { "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.6" } }, "nbformat": 4, "nbformat_minor": 0 }