{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"name": "W1_S1.ipynb",
"provenance": [],
"toc_visible": true,
"include_colab_link": true
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"language_info": {
"name": "python"
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"
"
]
},
{
"cell_type": "markdown",
"source": [
"# (Exercises) Text and Tabular Files"
],
"metadata": {
"id": "uZDBXWVc8XX6"
}
},
{
"cell_type": "markdown",
"source": [
"## Warm-up"
],
"metadata": {
"id": "avZmKWAAFdq7"
}
},
{
"cell_type": "markdown",
"source": [
"**1. Write a `while` loop to display the values of the ratings of an album playlist stored in the list PlayListRatings
. If the score is less than 6, exit the loop. The list PlayListRatings
is given by: PlayListRatings = [10, 9.5, 10, 8, 7.5, 5, 10, 10]
**"
],
"metadata": {
"id": "jxhHgmH8uTYf"
}
},
{
"cell_type": "code",
"source": [
"# Write your code here\n",
"PlayListRating = [10, 9.5, 10, 8, 7.5, 5, 10, 10]\n",
"\n",
"# Hint: Use list indexing as a criteria (size of your list)\n",
"indx = 0\n",
"while ____squares=['red', 'yellow', 'green', 'purple', 'blue']**"
],
"metadata": {
"id": "ORdNyphLwPj0"
}
},
{
"cell_type": "code",
"source": [
"squares = [] # An empty list\n",
"for ___,___ in enumerate([_____________________]):\n",
" squares.append(___) # Here you add (append) the elements one by one to the empty list\n",
"\n",
"print(squares)"
],
"metadata": {
"id": "dH7uOieswQ_t"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"Below, we import Python libraries that contain classes/functions that will be useful to both exercises."
],
"metadata": {
"id": "4Lc6G3ssy3Td"
}
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "xreaix9UpPl9"
},
"outputs": [],
"source": [
"import csv\n",
"import pandas as pd\n",
"import numpy as np"
]
},
{
"cell_type": "markdown",
"source": [
"## Exercise 1 (Text File)\n",
"Let's use what we have just learned to write a slightly more complex file.\n",
"1. Create a file called \"ans.txt\"\n",
"2. Write the following sentence \"This is my first I/O exercise.\"\n",
"3. Save the file\n",
"4. Read the file again, add 3 more new sentences: \"I just remembered that pi is approximately 3.1416\", \"Writing this to this file \", \"is a piece of cake!\"\n",
"\n",
"It is impossible to remember the exact value of pi, but we may want to change the accuracy of the pi we print at will. So let's import [math](https://docs.python.org/3/library/math.html) to get the exact pi value to output pi with the desired accuracy.\n",
"\n"
],
"metadata": {
"id": "tvemhVDuyB2h"
}
},
{
"cell_type": "markdown",
"source": [
""
],
"metadata": {
"id": "JfGTzshipDui"
}
},
{
"cell_type": "markdown",
"source": [
"Image by Bernadette Wurzinger from Pixabay"
],
"metadata": {
"id": "CQgM1TQaPPjU"
}
},
{
"cell_type": "markdown",
"source": [
"```{hint}\n",
"How can we change accuracy? Just change the integer that comes before f!\n",
":1f => 0.1,\n",
":2f => 0.11, ...\n",
"```"
],
"metadata": {
"id": "sA_yy9RDZVEI"
}
},
{
"cell_type": "markdown",
"source": [
"### Code"
],
"metadata": {
"id": "uJ-rr3MHZ0kd"
}
},
{
"cell_type": "code",
"source": [
"import math\n",
"# Hints: Please don't remove the \\n that your TA carefully put in the code!\n",
"with open(________, '_', encoding=\"utf-8\") as fhandler:\n",
" fhandler.write('______________________\\n')\n",
"\n",
"with open(________, '__', encoding=\"utf-8\") as fhandler:\n",
" print(fhandler.readlines())\n",
" # Your TA would like accuracy to the fourth decimal here.\n",
" firstline = f'____________________________ {math.pi:._f}\\n'\n",
" secondline = '_____________________________\\n'\n",
" thirdline = '___________________________\\n'\n",
" fhandler.writelines([___,____,___])\n",
" # Go to the beginning of file\n",
" fhandler.seek(0)\n",
" # Print the content of file\n",
" print(fhandler.readlines())"
],
"metadata": {
"id": "5rdfnOhSyGK3"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"## Exercise 2 (Tabular File)\n",
"In the demonstration we showed you how to extract 5 rows and write them to a new CSV file. Here, we provide you with data rows that encode surface weather observations recorded every hour. The data span from January to August.\n",
"\n",
"What if we want to extract January, February, and March data and save them in smaller files separated by month?\n",
"\n",
"Here we provide you with some functions to extract the indices of the first and final instance of the data on a given month in the tabular data. You don't need to change anything in these functions; in this exercise, you just need to save the monthly files based on the output indices."
],
"metadata": {
"id": "K6sYdxoG0kci"
}
},
{
"cell_type": "markdown",
"source": [
""
],
"metadata": {
"id": "8OUR3UUtqNfL"
}
},
{
"cell_type": "markdown",
"source": [
"Image by Tobias Hämmer from Pixabay"
],
"metadata": {
"id": "xq0ddLgcPSBh"
}
},
{
"cell_type": "code",
"source": [
"import pooch\n",
"import urllib.request\n",
"\n",
"#url = 'https://unils-my.sharepoint.com/:x:/g/personal/tom_beucler_unil_ch/ETDZdgCkWbZLiv_LP6HKCOAB2NP7H0tUTLlP_stknqQHGw?e=2lFo1x'\n",
"\n",
"datafile = pooch.retrieve('https://unils-my.sharepoint.com/:x:/g/personal/tom_beucler_unil_ch/ETDZdgCkWbZLiv_LP6HKCOAB2NP7H0tUTLlP_stknqQHGw?download=1',\n",
" known_hash='c7676360997870d00a0da139c80fb1b6d26e1f96050e03f2fed75b921beb4771')"
],
"metadata": {
"id": "S_qwRRC81gUu"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
""
],
"metadata": {
"id": "eKQ8EP5jqRfP"
}
},
{
"cell_type": "markdown",
"source": [
"Image by Erich Westendarp from Pixabay"
],
"metadata": {
"id": "6yzsvBGYPaJ1"
}
},
{
"cell_type": "markdown",
"source": [
"Complete the code below with the appropriate filename:"
],
"metadata": {
"id": "vw4jn5ZKaeDZ"
}
},
{
"cell_type": "code",
"source": [
"row = [] # Initializes row to an empty list\n",
"with open(___________, 'r') as fh:\n",
" reader = csv.reader(fh)\n",
" for info in reader:\n",
" row.append(info)"
],
"metadata": {
"id": "XTGi8oph0ljb"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"You can simply execute the two following cells:"
],
"metadata": {
"id": "upB7pRGyaijc"
}
},
{
"cell_type": "code",
"source": [
"def output_monthindices(month=None):\n",
" \"\"\"\n",
" This function takes a string \"month\" as input (e.g., January)\n",
" and outputs the first and last indices of that month\n",
" \"\"\"\n",
" test = [rowobj[1].split(' ')[0].split('-')[1] for rowobj in row[1:]]\n",
" truefalse = []\n",
" for obj in test:\n",
" if obj==month:\n",
" truefalse.append(obj)\n",
" else:\n",
" truefalse.append(np.nan)\n",
" return pd.Series(truefalse).first_valid_index(),pd.Series(truefalse).last_valid_index()"
],
"metadata": {
"id": "d7zcnfqQ1r4N"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"# Here, we output the first and last indices of Jan/Feb/Mar\n",
"Jan_index = output_monthindices(month='01')\n",
"Feb_index = output_monthindices(month='02')\n",
"Mar_index = output_monthindices(month='03')"
],
"metadata": {
"id": "AH_w33VM1xbT"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"Here, let's extract portions of the `row` list with the monthly index values we have.\n",
"\n",
"Write these extracts to three new files: `jan.csv`,`feb.csv`,`mar.csv` using a loop."
],
"metadata": {
"id": "PfoOfmTt2UZX"
}
},
{
"cell_type": "code",
"source": [
"savefile = [_,_,_] # List containing the filenames\n",
"indices = [Jan_index, Feb_index, Mar_index]\n",
"for i in range(_):\n",
" with open(__, 'w') as fh:\n",
" writer = csv.writer(fh)\n",
" for ____ in range(indices[_][0],indices[_][1]):\n",
" writer.writerow(row[____])"
],
"metadata": {
"id": "jEqKsC902Irp"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"#@title Answer\n",
"savefile = ['jan.csv','feb.csv','mar.csv']\n",
"indices = [Jan_index, Feb_index, Mar_index]\n",
"for i in range(3):\n",
" with open(savefile[i], 'w') as fh:\n",
" writer = csv.writer(fh)\n",
" for num in range(indices[i][0],indices[i][1]):\n",
" writer.writerow(row[num])"
],
"metadata": {
"id": "VVSBDr4r1ztk",
"cellView": "form"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"Let's check that we properly wrote our weather data to the appropriate file. Just run the cell below to check 😃"
],
"metadata": {
"id": "5VnxWCNqztc4"
}
},
{
"cell_type": "code",
"source": [
"#@title Let's print the dates in March\n",
"df = pd.read_csv(savefile[2]) # Reads the file using pandas\n",
"df.head(3) # Print the first three lines of the file"
],
"metadata": {
"id": "i_OLSvLZ0Otd"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"Congratulations! You just finished some programming tasks with Python!\n",
"Excited? Stay along for the ride, we have more exercises for you in the next section!\n",
"Our next exercise will be on our solar system! 🪐"
],
"metadata": {
"id": "g26-XTMKqZWQ"
}
}
]
}