1.2. (Exercises) Text and Tabular Files#
1.2.1. Warm-up#
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]
# Write your code here
PlayListRating = [10, 9.5, 10, 8, 7.5, 5, 10, 10]
# Hint: Use list indexing as a criteria (size of your list)
indx = 0
while ____<len(______________):
if ____________[____]<6:
____
else:
print(_________[____])
____+=1
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
Cell In[1], line 6
4 # Hint: Use list indexing as a criteria (size of your list)
5 indx = 0
----> 6 while ____<len(______________):
7 if ____________[____]<6:
8 ____
NameError: name '____' is not defined
2. Repeat what you just did, this time using a for
loop
for ____ in range(len(______________)):
if _____________[____]<6:
____
else:
print(_____________[____])
3. Write a for
loop that prints out the following list: squares=[‘red’, ‘yellow’, ‘green’, ‘purple’, ‘blue’]
squares = [] # An empty list
for ___,___ in enumerate([_____________________]):
squares.append(___) # Here you add (append) the elements one by one to the empty list
print(squares)
Below, we import Python libraries that contain classes/functions that will be useful to both exercises.
import csv
import pandas as pd
import numpy as np
1.2.2. Exercise 1 (Text File)#
Let’s use what we have just learned to write a slightly more complex file.
Create a file called “ans.txt”
Write the following sentence “This is my first I/O exercise.”
Save the file
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!”
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 to get the exact pi value to output pi with the desired accuracy.
Image by Bernadette Wurzinger from Pixabay
Hint
How can we change accuracy? Just change the integer that comes before f! :1f => 0.1, :2f => 0.11, …
1.2.2.1. Code#
import math
# Hints: Please don't remove the \n that your TA carefully put in the code!
with open(________, '_', encoding="utf-8") as fhandler:
fhandler.write('______________________\n')
with open(________, '__', encoding="utf-8") as fhandler:
print(fhandler.readlines())
# Your TA would like accuracy to the fourth decimal here.
firstline = f'____________________________ {math.pi:._f}\n'
secondline = '_____________________________\n'
thirdline = '___________________________\n'
fhandler.writelines([___,____,___])
# Go to the beginning of file
fhandler.seek(0)
# Print the content of file
print(fhandler.readlines())
1.2.3. Exercise 2 (Tabular File)#
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.
What if we want to extract January, February, and March data and save them in smaller files separated by month?
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.
Image by Tobias Hämmer from Pixabay
import pooch
import urllib.request
#url = 'https://unils-my.sharepoint.com/:x:/g/personal/tom_beucler_unil_ch/ETDZdgCkWbZLiv_LP6HKCOAB2NP7H0tUTLlP_stknqQHGw?e=2lFo1x'
datafile = pooch.retrieve('https://unils-my.sharepoint.com/:x:/g/personal/tom_beucler_unil_ch/ETDZdgCkWbZLiv_LP6HKCOAB2NP7H0tUTLlP_stknqQHGw?download=1',
known_hash='c7676360997870d00a0da139c80fb1b6d26e1f96050e03f2fed75b921beb4771')
Image by Erich Westendarp from Pixabay
Complete the code below with the appropriate filename:
row = [] # Initializes row to an empty list
with open(___________, 'r') as fh:
reader = csv.reader(fh)
for info in reader:
row.append(info)
You can simply execute the two following cells:
def output_monthindices(month=None):
"""
This function takes a string "month" as input (e.g., January)
and outputs the first and last indices of that month
"""
test = [rowobj[1].split(' ')[0].split('-')[1] for rowobj in row[1:]]
truefalse = []
for obj in test:
if obj==month:
truefalse.append(obj)
else:
truefalse.append(np.nan)
return pd.Series(truefalse).first_valid_index(),pd.Series(truefalse).last_valid_index()
# Here, we output the first and last indices of Jan/Feb/Mar
Jan_index = output_monthindices(month='01')
Feb_index = output_monthindices(month='02')
Mar_index = output_monthindices(month='03')
Here, let’s extract portions of the row
list with the monthly index values we have.
Write these extracts to three new files: jan.csv
,feb.csv
,mar.csv
using a loop.
savefile = [_,_,_] # List containing the filenames
indices = [Jan_index, Feb_index, Mar_index]
for i in range(_):
with open(__, 'w') as fh:
writer = csv.writer(fh)
for ____ in range(indices[_][0],indices[_][1]):
writer.writerow(row[____])
#@title Answer
savefile = ['jan.csv','feb.csv','mar.csv']
indices = [Jan_index, Feb_index, Mar_index]
for i in range(3):
with open(savefile[i], 'w') as fh:
writer = csv.writer(fh)
for num in range(indices[i][0],indices[i][1]):
writer.writerow(row[num])
Let’s check that we properly wrote our weather data to the appropriate file. Just run the cell below to check 😃
#@title Let's print the dates in March
df = pd.read_csv(savefile[2]) # Reads the file using pandas
df.head(3) # Print the first three lines of the file
Congratulations! You just finished some programming tasks with Python! Excited? Stay along for the ride, we have more exercises for you in the next section! Our next exercise will be on our solar system! 🪐