{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Lecture 3C—Plain Text File Reading and Writing" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In this lecture, we will explore reading and writing plain text files.\n", "\n", "We will learn:\n", "\n", "- [Read TXT file](#Reading-plain-text-file)\n", "- [Write TXT file](#Example-of-writing-plain-text-file:-Diary-logging)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Reading plain text file" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can use `open()` to read and write plain text file. There are 3 modes when opening a file: \n", "\n", "- `r` for reading.\n", "- `w` for over-writing.\n", "- `a` for appending." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "👇🏻 Please note that we need to specify `encoding=\"utf-8\"` in Windows for the `open` function." ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "scrolled": true }, "outputs": [ { "data": { "text/plain": [ "['I want to put a ding in the universe.—Steve Jobs',\n", " 'Life is 10% what happens to you and 90% how you react to it.—Charles R. Swindoll',\n", " \"Family is not an important thing. It's everything.—Michael J. Fox\",\n", " \"Nothing is impossible, the word itself says 'I'm possible'!—Audrey Hepburn\",\n", " 'There are two ways of spreading light: to be the candle or the mirror that reflects it.—Edith Wharton',\n", " \"Try to be a rainbow in someone's cloud.—Maya Angelou\",\n", " 'Be brave enough to live life creatively. The creative place where no one else has ever been.—Alan Alda',\n", " 'The secret of getting ahead is getting started.—Mark Twain']" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "with open(\"quotes.txt\") as file_obj:\n", " quotes = file_obj.read().splitlines()\n", " \n", "quotes" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['I want to put a ding in the universe.—Steve Jobs',\n", " 'Life is 10% what happens to you and 90% how you react to it.—Charles R. Swindoll',\n", " \"Family is not an important thing. It's everything.—Michael J. Fox\",\n", " \"Nothing is impossible, the word itself says 'I'm possible'!—Audrey Hepburn\",\n", " 'There are two ways of spreading light: to be the candle or the mirror that reflects it.—Edith Wharton',\n", " \"Try to be a rainbow in someone's cloud.—Maya Angelou\",\n", " 'Be brave enough to live life creatively. The creative place where no one else has ever been.—Alan Alda',\n", " 'The secret of getting ahead is getting started.—Mark Twain']" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "with open(\"quotes.txt\", encoding=\"utf-8\") as file_obj:\n", " quotes = file_obj.read().splitlines()\n", " \n", "quotes" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Family is not an important thing. It's everything.—Michael J. Fox\n" ] } ], "source": [ "import random\n", "\n", "quote = random.choice(quotes)\n", "\n", "print(quote)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Example of writing plain text file: Diary logging\n" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "What do you want to say to Mr. Diary? Hello python.\n", "2020-06-11: Hello\n", "2020-06-11: Hello\n", "2020-08-10: Hello python.\n" ] } ], "source": [ "import datetime\n", "\n", "content = input(\"What do you want to say to Mr. Diary? \")\n", "if len(content) > 0:\n", " with open('diary.txt', \"a\") as file_obj:\n", " today = datetime.date.today().isoformat()\n", " file_obj.write(today + \": \" + content + \"\\n\")\n", "\n", "with open('diary.txt', \"r\") as file_obj:\n", " lines = file_obj.readlines()\n", " for line in lines[-3:]:\n", " print(line.rstrip())\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Summary\n", "\n", "In this section, we learned to read and write plain text file. " ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.6" } }, "nbformat": 4, "nbformat_minor": 2 }