{ "cells": [ { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "# Lecture 15: Other file formats\n", "\n", "CSCI 1360: Foundations for Informatics and Analytics" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Overview and Objectives" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "In the last lecture, we looked at some ways of interacting with the filesystem through Python and how to read data off files stored on the hard drive. We looked at raw text files; however, there are numerous structured formats that these files can take, and we'll explore some of those here. By the end of this lecture, you should be able to:" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ " - Identify some of the primary data storage formats\n", " - Explain how to use other tools for some of the more exotic data types" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Part 1: Comma-separated value (CSV) files" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "We've discussed text formats: each line of text in a file can be treated as a string in a list of strings. What else might we encounter in our data science travels?" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "Easily the most common text file format is the CSV, or comma-separated values format. This is pretty much what it sounds like: if you have (semi-) structured data, you can delineate spaces between data using commas (or, to generalize, other characters like tabs)." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "As an example, we could represent a matrix very easily using the CSV format. The file storing a 3x3 matrix would look something like this:" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "
\n", "1,2,3\n", "4,5,6\n", "7,8,9\n", "" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "Each row is on one line by itself, and the columns are separated by commas." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "How can we read a CSV file? One way, potentially, is just do it yourself:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true, "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "# File \"csv_file.txt\" contains the following:\n", "\n", "# 1,2,3,4\n", "# 5,6,7,8\n", "# 9,10,11,12" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]\n" ] } ], "source": [ "matrix = []\n", "\n", "with open(\"csv_file.txt\", \"r\") as f:\n", " full_file = f.read()\n", " \n", " # Split into lines.\n", " lines = full_file.strip().split(\"\\n\")\n", "\n", " for line in lines:\n", " # Split on commas.\n", " elements = line.strip().split(\",\")\n", " matrix.append([])\n", " \n", " # Convert to integers and store in the list.\n", " for e in elements:\n", " matrix[-1].append(int(e))\n", "\n", "print(matrix)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "If, however, we'd prefer to use something a little less `strip()`-y and `split()`-y, Python also has a core `csv` module built-in:" ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Sunny-side up,Over easy,Scrambled\n", "Spam,Spam,More spam\n", "\n" ] } ], "source": [ "import csv\n", "\n", "with open(\"eggs.csv\", \"w\") as csv_file:\n", " file_writer = csv.writer(csv_file)\n", " row1 = [\"Sunny-side up\", \"Over easy\", \"Scrambled\"]\n", " row2 = [\"Spam\", \"Spam\", \"More spam\"]\n", " file_writer.writerow(row1)\n", " file_writer.writerow(row2)\n", " \n", "with open(\"eggs.csv\", \"r\") as csv_file:\n", " print(csv_file.read())" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "Notice that you first create a file reference, just like before. The one added step, though, is passing that reference to the `csv.writer()` function." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "Once you've created the `file_writer` object, you can call its `writerow()` function and pass in a list to the function, and it is automatically written to the file in CSV format!" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "The CSV readers let you do the opposite: read a line of text from a CSV file directly into a list." ] }, { "cell_type": "code", "execution_count": 15, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['Sunny-side up', 'Over easy', 'Scrambled']\n", "['Spam', 'Spam', 'More spam']\n" ] } ], "source": [ "with open(\"eggs.csv\", \"r\") as csv_file:\n", " file_reader = csv.reader(csv_file)\n", " for csv_row in file_reader:\n", " print(csv_row)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "You can use a `for` loop to iterate over the rows in the CSV file. In turn, each row is a list, where each element of the list was separated by a comma." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Part 2: JavaScript Object Notation (JSON) files" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "\"JSON\", short for \"JavaScript Object Notation\", has emerged as more or less the *de facto* standard format for interacting with online services. Like CSV, it's a text-based format, but is much more flexible than CSV." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "Here's an example: an object in JSON format that represents a person." ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "collapsed": true, "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "person = \"\"\"\n", "{\"name\": \"Wes\",\n", "\"places_lived\": [\"United States\", \"Spain\", \"Germany\"],\n", "\"pet\": null,\n", "\"siblings\": [{\"name\": \"Scott\", \"age\": 25, \"pet\": \"Zuko\"},\n", " {\"name\": \"Katie\", \"age\": 33, \"pet\": \"Cisco\"}]\n", "}\n", "\"\"\"" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "It looks kind of a like a Python dictionary, doesn't it? You have key-value pairs, and they can accommodate almost any data type. In fact, when JSON objects are converted into native Python data structures, they are represented using dictionaries." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "For reading and writing JSON objects, we can use the built-in `json` Python module." ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "collapsed": true, "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "import json" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "(*Aside*: with CSV files, it was fairly straightforward to eschew the built-in `csv` module and do it yourself. With JSON, it is **much harder**; in fact, there really isn't a case where it's advisable to roll your own over using the built-in `json` module)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "There are two functions of interest: `dumps()` and `loads()`. One of them takes a JSON string and converts it to a native Python object, while the other does the opposite." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "First, we'll take our JSON string and convert it into a Python dictionary:" ] }, { "cell_type": "code", "execution_count": 15, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{'places_lived': ['United States', 'Spain', 'Germany'], 'pet': None, 'name': 'Wes', 'siblings': [{'age': 25, 'pet': 'Zuko', 'name': 'Scott'}, {'age': 33, 'pet': 'Cisco', 'name': 'Katie'}]}\n" ] } ], "source": [ "python_dict = json.loads(person)\n", "print(python_dict)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "And if you want to take a Python dictionary and convert it into a JSON string--perhaps you're about to save it to a file, or send it over the network to someone else--we can do that." ] }, { "cell_type": "code", "execution_count": 16, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{\"places_lived\": [\"United States\", \"Spain\", \"Germany\"], \"pet\": null, \"name\": \"Wes\", \"siblings\": [{\"age\": 25, \"pet\": \"Zuko\", \"name\": \"Scott\"}, {\"age\": 33, \"pet\": \"Cisco\", \"name\": \"Katie\"}]}\n" ] } ], "source": [ "json_string = json.dumps(python_dict)\n", "print(json_string)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "At first glance, these two print-outs may look the same, but if you look closely you'll see some differences. Plus, if you tried to index `json_string[\"name\"]` you'd get some very strange errors. `python_dict[\"name\"]`, on the other hand, should nicely return `\"Wes\"`." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Part 3: Extensible Markup Language (XML) files" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "**AVOID AT ALL COSTS**." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "...but if you have to interact with XML data (e.g., you're manually parsing a web page!), Python has a built-in `xml` library." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "XML is about as general as it gets when it comes to representing data using structured text; you can represent pretty much anything. HTML is an example of XML in practice." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "```\n", "\n", "