Lab 3: Processing Data in Files: Learning Goals
Lab 3: Processing Data in Files: Learning Goals
Learning Goals
to process data stored in files to read or write data to a file
Main Lab
Download the file yvr_2011.csv. This file comes to us courtesy of Environment Canada and contains observed weather data at Vancouver International for every day of the year. This fileis a copy of an official work published by the Government of Canada. It has not been reproduced in affiliation with or with the endorsement of the Government of Canada. The data is provided in accordance with the Copyright / Permission to Reproduce notice posted by Environment Canada. Download the file lorumIpsum.txt. This file comes to you courtesy of http://www.lipsum.com/. Create a Python project named Lab 3 and be sure to create a source folder named src. Drag the file yvr_2011.csv and drop it onto the src folder. Do the same with the file lorumIpsum.txt. Open the file yvr_2011.csv using Excel (if you have it available) or directly from within Eclipse and note that it has a certain number of header rows and a certain number of columns containing data. Be careful not to modify the file in any way - do not save it from Excel or from text editor! When designing your functions in this lab you must allow for the possibility that the number of header rows may change in future. You must also allow for the possibility that sometime in the future Environment Canada chooses to use a different delimiter to separate data in the file. At the moment, values are separated by a comma. Open lorumIpsum.txt from within Eclipse and note that it contains a bunch of text in Latin. Be careful not to modify the file in any way - do not save it from the text editor! Download the starter file lab03.py and work through the exercises provided.
How to set the working directory in a PyDev console When you open a PyDev console in this lab, you will need to set the working directory to be your project's src file so that you can easily specify the data files to be opened. To do this: 1) start the PyDev console and enter:
https://sites.google.com/site/ubccpsc189/labs/lab-3
import os 2) right-click your project's src folder and select Properties from the pop-up menu. 3) highlight the location path and copy it 4) back in the PyDev console enter: os.chdir('') and paste the path that you copied in step (3) between the quote marks. The chdir function changes the working directory to the one specified by the path that you pass as a parameter. How to reload a module after making changes It's important to note that a module needs to be reloaded in the PyDev console whenever you make a change to it (e.g., edit a function or add a new function). However, before you can reload it, it must already have been imported. So be sure to import the module, in addition to loading particular functions from the module: import my_module from my_module import my_function If you then make a change to my_function or add another function to my_module, you need to reload the module as follows: reload(my_module) then from my_module import my_function or, if you've added a new function and want to use it: from my_module import my_new_function # <---- important
lab3.py (8k) lorumIpsum.txt (60k) yvr_2011.csv (25k) Paul Carter, Jan 18, 2013, 11:23 AM Paul Carter, Jan 17, 2013, 2:12 PM Paul Carter, Jan 17, 2013, 12:36 PM v.4 v.1 v.1
https://sites.google.com/site/ubccpsc189/labs/lab-3