{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### <center>San Jose State University<br>Department of Applied Data Science<br><br>**DATA 200<br>Computational Programming for Data Analytics**<br><br>Spring 2023<br>Instructor: Ron Mak</center>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 9.3.2 Reading Data from a Text File"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Keyword argument `mode='r'` specifies to **read** an existing file."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "with open('accounts.txt', mode='r') as accounts:\n",
    "    print(f'{\"Account\":<10}{\"Name\":<7}{\"Balance\":>10}')\n",
    "    for record in accounts:\n",
    "        account, name, balance = record.split()\n",
    "        print(f'{account:^10}{name:<7}{balance:>10}')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### File Method `readlines`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "accounts_file = open('accounts.txt', mode='r')\n",
    "print(f'{\"Account\":<10}{\"Name\":<7}{\"Balance\":>10}')\n",
    "\n",
    "record = accounts_file.readline()\n",
    "\n",
    "while record != '':\n",
    "    account, name, balance = record.split()\n",
    "    print(f'{account:^10}{name:<7}{balance:>10}')\n",
    "    \n",
    "    record = accounts_file.readline()\n",
    "    \n",
    "accounts_file.close()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Read one character at a time\n",
    "#### Here, we use `read()` to read and print first 10 characters of the file."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "accounts_file = open('accounts.txt', mode='r')\n",
    "\n",
    "for position in range(0, 10):\n",
    "    ch = accounts_file.read(1)\n",
    "    print(f'{position = }  {ch = }')\n",
    "    \n",
    "accounts_file.close()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Seeking to a Specific File Position\n",
    "#### The first character of the file is at position 0."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "accounts_file = open('accounts.txt', mode='r')\n",
    "accounts_file.seek(4)\n",
    "\n",
    "print(f'{accounts_file.read(5) = }')\n",
    "accounts_file.close()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### File modes"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "| Mode | Description |\n",
    "| --- | --- |\n",
    "| `'r'` | Open a text file for reading. This is the default. |\n",
    "| `'w'` | Open a text file for writing. The file's current contents are deleted. Create the file if it doesn't already exist.|\n",
    "| `'a'` | Open a text file for writing new data at the end. Create the file if it doesn't already exist. |\n",
    "| `'r+'` | Open a text file for reading and writing. |\n",
    "| `'w+'` | Open a text file for reading and writing. The file's current contents are deleted. |\n",
    "| `'a+'` | Open a text file for reading and writing at the end. Create the file if it doesn't already exist. |"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "##########################################################################\n",
    "# (C) Copyright 2019 by Deitel & Associates, Inc. and                    #\n",
    "# Pearson Education, Inc. All Rights Reserved.                           #\n",
    "#                                                                        #\n",
    "# DISCLAIMER: The authors and publisher of this book have used their     #\n",
    "# best efforts in preparing the book. These efforts include the          #\n",
    "# development, research, and testing of the theories and programs        #\n",
    "# to determine their effectiveness. The authors and publisher make       #\n",
    "# no warranty of any kind, expressed or implied, with regard to these    #\n",
    "# programs or to the documentation contained in these books. The authors #\n",
    "# and publisher shall not be liable in any event for incidental or       #\n",
    "# consequential damages in connection with, or arising out of, the       #\n",
    "# furnishing, performance, or use of these programs.                     #\n",
    "##########################################################################\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Additional material (C) Copyright 2023 by Ronald Mak"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "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.11.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
