diff --git a/python-data/files.ipynb b/python-data/files.ipynb
index a690a88..1bb805a 100644
--- a/python-data/files.ipynb
+++ b/python-data/files.ipynb
@@ -1,125 +1,135 @@
{
- "metadata": {
- "name": "",
- "signature": "sha256:f23d7a0b01d6b36ad881c51726d1c5e72a1c2ee783560c6d984d2742ed199266"
- },
- "nbformat": 3,
- "nbformat_minor": 0,
- "worksheets": [
+ "cells": [
{
- "cells": [
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "# Files\n",
+ "\n",
+ "* Read a File\n",
+ "* Write a File\n",
+ "* Read and Write UTF-8"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Read a File\n",
+ "\n",
+ "Open a file in read-only mode.
\n",
+ "Iterate over the file lines. rstrip removes the EOL markers.
"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 1,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [
{
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "# Files\n",
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "class TypeUtil:\n",
"\n",
- "* Read a File\n",
- "* Write a File\n",
- "* Read and Write UTF-8"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "## Read a File\n",
+ " @classmethod\n",
+ " def is_iterable(cls, obj):\n",
+ " \"\"\"Determines if obj is iterable.\n",
"\n",
- "Open a file in read-only mode.
\n",
- "Iterate over the file lines. rstrip removes the EOL markers.
"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "old_file_path = 'type_util.py'\n",
- "with open(old_file_path, 'r') as old_file:\n",
- " for line in old_file:\n",
- " print(line.rstrip())"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "class TypeUtil:\n",
- "\n",
- " @classmethod\n",
- " def is_iterable(cls, obj):\n",
- " \"\"\"Determines if obj is iterable.\n",
- "\n",
- " Useful when writing functions that can accept multiple types of\n",
- " input (list, tuple, ndarray, iterator). Pairs well with\n",
- " convert_to_list.\n",
- " \"\"\"\n",
- " try:\n",
- " iter(obj)\n",
- " return True\n",
- " except TypeError:\n",
- " return False\n",
- "\n",
- " @classmethod\n",
- " def convert_to_list(cls, obj):\n",
- " \"\"\"Converts obj to a list if it is not a list and it is iterable,\n",
- " else returns the original obj.\n",
- " \"\"\"\n",
- " if not isinstance(obj, list) and cls.is_iterable(obj):\n",
- " obj = list(obj)\n",
- " return obj\n"
- ]
- }
- ],
- "prompt_number": 1
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "## Write to a file\n",
+ " Useful when writing functions that can accept multiple types of\n",
+ " input (list, tuple, ndarray, iterator). Pairs well with\n",
+ " convert_to_list.\n",
+ " \"\"\"\n",
+ " try:\n",
+ " iter(obj)\n",
+ " return True\n",
+ " except TypeError:\n",
+ " return False\n",
"\n",
- "Create a new file overwriting any previous file with the same name, write text, then close the file:"
+ " @classmethod\n",
+ " def convert_to_list(cls, obj):\n",
+ " \"\"\"Converts obj to a list if it is not a list and it is iterable,\n",
+ " else returns the original obj.\n",
+ " \"\"\"\n",
+ " if not isinstance(obj, list) and cls.is_iterable(obj):\n",
+ " obj = list(obj)\n",
+ " return obj\n"
]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "new_file_path = 'hello_world.txt'\n",
- "with open(new_file_path, 'w') as new_file:\n",
- " new_file.write('hello world!')"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [],
- "prompt_number": 2
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "## Read and Write UTF-8"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "import codecs\n",
- "with codecs.open(\"hello_world_new.txt\", \"a\", \"utf-8\") as new_file:\n",
- " with codecs.open(\"hello_world.txt\", \"r\", \"utf-8\") as old_file: \n",
- " for line in old_file:\n",
- " new_file.write(line + '\\n')"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [],
- "prompt_number": 3
}
],
- "metadata": {}
+ "source": [
+ "old_file_path = 'type_util.py'\n",
+ "with open(old_file_path, 'r') as old_file:\n",
+ " for line in old_file:\n",
+ " print(line.rstrip())"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Write to a file\n",
+ "\n",
+ "Create a new file overwriting any previous file with the same name, write text, then close the file:"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 2,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [],
+ "source": [
+ "new_file_path = 'hello_world.txt'\n",
+ "with open(new_file_path, 'w') as new_file:\n",
+ " new_file.write('hello world!')"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Read and Write UTF-8"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 3,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [],
+ "source": [
+ "import codecs\n",
+ "with codecs.open(\"hello_world_new.txt\", \"a\", \"utf-8\") as new_file:\n",
+ " with codecs.open(\"hello_world.txt\", \"r\", \"utf-8\") as old_file: \n",
+ " for line in old_file:\n",
+ " new_file.write(line + '\\n')"
+ ]
}
- ]
-}
\ No newline at end of file
+ ],
+ "metadata": {
+ "kernelspec": {
+ "display_name": "Python 2",
+ "language": "python",
+ "name": "python2"
+ },
+ "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.9"
+ }
+ },
+ "nbformat": 4,
+ "nbformat_minor": 0
+}