{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "Course 5: Input and output\n", "==========================\n", "\n", "In this worksheet we learn how to read and write files. We will also learn more string manipulation.\n", "\n", "Prerequisites (...need to open a terminal...):\n", "- filesystem (the commands `cd`, `ls`, `pwd` in terminal)\n", "- relative versus absolute paths\n", "- special repositories `./` and `../`\n", "\n", "The main function to deal with files is the function `open`. When using it you have to specify a mode that can be `'w'` for writing or `'r'` for reading. **When you open a file in writing mode you erase its content!**\n", "\n", "Below is an example of a creation of a file in the repository `/tmp/` (that is intended to be used for temporary files)\n", "```python\n", "f = open(\"/tmp/hello.txt\", \"w\") # open in write mode\n", "f.write(\"I am writing to a file\\n\") # write a string in the file\n", "f.write(\"it is cool!\\n\") # write a string in the file\n", "f.close() # closing the file\n", "```\n", "It is important to close the file (the last step above) since otherwise some data might not have been written in the file (because of buffers). If you have a file on the disc then you can read its content with\n", "```python\n", "# opening file in read mode\n", "g = open(\"/tmp/hello.txt\", \"r\") # open in read mode\n", "text = g.read() # gettint content as a string\n", "g.close() # closing the file\n", "```\n", "**Exercise:**\n", "- execute the first set of commands above that creates the file `/tmp/hello.txt`\n", "- check with nautilus or the terminal that this file does exist on your computer (you can even open it and edit it with gedit)\n", "- execute the second set of commands above that reads the content of this file `/tmp/hello.txt`\n", "- print the content of `text`" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "It is important to know about more string operations to treat data contained in a file. You already learned about concatenation (the `+` operation). We now learn about the \"opposite\" operation. If you have a string `s` the command `s.split(sep)` cut it into pieces along the character `sep`. You can execute the following examples\n", "```python\n", "\"how are you doing today?\".split(\" \")\n", "\"abaabaaababa\".split(\"b\")\n", "```\n", "\n", "**Exercise:**\n", "- Let `s1` be defined as below\n", "```python\n", "s1 = \"Alfred,Eistein\\nAlexandre,Grothendieck\\nBernhard,Riemann\"\n", "```\n", "- Print the above string.\n", "- Using `split` or `splitlines` lines transform `s1` into the list\n", "```python\n", "['Alfred,Eistein', 'Alexandre,Grothendieck', 'Bernhard,Riemann']\n", "```\n", "- Using `split` transform `s1` into the list\n", "```python\n", "[['Alfred', 'Eistein'], ['Alexandre', 'Grothendieck'], ['Bernhard', 'Riemann']]\n", "```" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Exercise:**\n", "- Download the file [names.csv](https://raw.githubusercontent.com/videlec/aims-python-rwanda-2016/master/worksheets/names.csv) and save it on your computer. Each line of this file consists in a first name, a name, a country, a birth date separated by a coma as in\n", "```\n", "Hammed,Gibbs,Zambia,01/08/1983\n", "Kais,Poisson,Portugal,09/07/1999\n", "```\n", "- Read this file\n", "- Using the method `split` of strings transform this string into a list of lists of the form\n", "```python\n", "[[\"Hammed\", \"Gibbs\", \"Zambia\", \"01\", \"08\", \"1983\"],\n", " [\"Kais\", \"Poisson\", \"Portugal\", \"09\", \"07\", \"1999\"]]\n", "```\n", "- How many persons in the list are from Zambia?\n", "- How many persons are borned in 1988?\n", "- Print the list of days and months so that more than one person have their birthday at this date." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Saving numpy arrays\n", "-------------------\n", "\n", "The `numpy` library introduced their own format to read arrays in a file. These are the functions `save` and `load` from the `numpy` library.\n", "\n", "Recall that in order to use numpy you need to import it and the standard way to do so is\n", "```python\n", "import numpy as np\n", "```\n", "Once numpy is imported you can save and load as follows\n", "```python\n", "a = np.arange(1, 10, 1) # creates an array\n", "np.save('my_object.npy', a) # saves the array in 'my_object.npy'\n", "b = np.load('my_object.npy') # loads the object saved and assigned it to b\n", "```\n", "**Exercise:**\n", "- Execute the above code\n", "- Download the files [array1.npy](https://github.com/videlec/aims-python-rwanda-2016/blob/master/worksheets/array1.npy?raw=true), [array2.npy](https://github.com/videlec/aims-python-rwanda-2016/blob/master/worksheets/array2.npy?raw=true) and [array3.npy](https://github.com/videlec/aims-python-rwanda-2016/blob/master/worksheets/array3.npy?raw=true).\n", "- For each of the file you downloaded, load them as a numpy array.\n", "- Each of these arrays was generated as a random sample of a distribution you know. Are you able to find which ones?" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Additional remarks\n", "------------------\n", "\n", "You might want to explore the filesystem from Python (instead of using the terminal or nautilus). There are tools for that purpose in the standard Python library (in particular [os.path](https://docs.python.org/3/library/os.path.html) and [pathlib](https://docs.python.org/3/library/pathlib.html)). For example\n", "```python\n", "import os\n", "os.listdir('.') # list the current directory\n", "os.path.abspath('.') # return the current directory as an absolute path\n", "```\n", "\n", "More information is available in the chapter [File and Directory Access](https://docs.python.org/3/library/filesys.html) of the standard Python library." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Copyright (C) 2016 Vincent Delecroix <vincent.delecroix@u-bordeaux.fr>\n", "\n", "This work is licensed under a Creative Commons Attribution-NonCommercial 4.0\n", "International License (CC BY-NC-SA 4.0). You can either read the\n", "[Creative Commons Deed](https://creativecommons.org/licenses/by-nc-sa/4.0/)\n", "(Summary) or the [Legal Code](https://creativecommons.org/licenses/by-nc-sa/4.0/legalcode)\n", "(Full licence)." ] } ], "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.10.6" } }, "nbformat": 4, "nbformat_minor": 1 }