From 8b974b811ace4f9756bf6e317815653972c2828b Mon Sep 17 00:00:00 2001 From: Donne Martin Date: Mon, 16 Feb 2015 17:01:10 -0500 Subject: [PATCH] Added snippet to read and write utf-8. --- core/files.ipynb | 33 ++++++++++++++++++++++++++++----- 1 file changed, 28 insertions(+), 5 deletions(-) diff --git a/core/files.ipynb b/core/files.ipynb index ab025c3..9454336 100644 --- a/core/files.ipynb +++ b/core/files.ipynb @@ -1,7 +1,7 @@ { "metadata": { "name": "", - "signature": "sha256:c7a4bcb2e55f64b6c49581b50c190fa5f6742f215ee7c4e3a6d10b11319a56fb" + "signature": "sha256:c535c4eb558966d14a178dfbd21918ecadc105f12b3c5f939ebf807d5afd1e6f" }, "nbformat": 3, "nbformat_minor": 0, @@ -16,7 +16,8 @@ "\n", "* Open a File for Reading\n", "* Read a File\n", - "* Write to a New File" + "* Write to a New File\n", + "* Read and write UTF-8" ] }, { @@ -38,7 +39,7 @@ "language": "python", "metadata": {}, "outputs": [], - "prompt_number": 1 + "prompt_number": 2 }, { "cell_type": "markdown", @@ -90,7 +91,7 @@ ] } ], - "prompt_number": 2 + "prompt_number": 3 }, { "cell_type": "markdown", @@ -113,7 +114,29 @@ "language": "python", "metadata": {}, "outputs": [], - "prompt_number": 3 + "prompt_number": 4 + }, + { + "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": 5 } ], "metadata": {}