Presentation is loading. Please wait.

Presentation is loading. Please wait.

Today… Files from the Web! Dictionaries. Lists of lists. Winter 2016CISC101 - Prof. McLeod1.

Similar presentations


Presentation on theme: "Today… Files from the Web! Dictionaries. Lists of lists. Winter 2016CISC101 - Prof. McLeod1."— Presentation transcript:

1 Today… Files from the Web! Dictionaries. Lists of lists. Winter 2016CISC101 - Prof. McLeod1

2 Files from the Web A file is a file, no matter where it comes from! See how you can read a file from anywhere in the web as long as you have the URL (or web address) for that file. CISC101 - Prof. McLeod2Winter 2016

3 Files from the Web Use the module urllib.request The function fh = urllib.request.urlopen(url_string) accepts a URL as a string and returns a handle to the file. (What does “URL” stand for?) The entire file can be read using (guess what!) the read() method: contents = fh.read() You can supply a number to the read() method to limit the number of bytes read. CISC101 - Prof. McLeod3Winter 2016

4 Files from the Web These are byte strings, which are better to decode using the decode() method. For example: contentsString = contents.decode() The decode method attempts to use the proper encoding, which is commonly “utf-8”, but there are others… (See: http://www.w3.org/International/O-charset for more information on encoding.) CISC101 - Prof. McLeod4Winter 2016

5 Files from the Web See the demo program URLFileDemo.py Just hints at what you can do! CISC101 - Prof. McLeod5Winter 2016

6 Dictionaries Or a “dict”. Another built-in collection type in Python. Mutable collection (like lists). A “mapped” data structure – not index based. Uses a set of key : value associations. You cannot use the slice operator with a dictionary, instead you use the key name. The key name goes in a set of [ ], but this is not the slice operator in the context of dictionaries. CISC101 - Prof. McLeod6Winter 2016

7 Dictionaries, Cont. Create an empty dictionary using: emptyDict = dict() or: emptyDict = { } Add or modify a key : value pair simply as: emptyDict['name'] = "Alan" emptyDict is now {'name': 'Alan'} Each key must be unique and hashable – strings are most common. Winter 2016CISC101 - Prof. McLeod7

8 Dictionaries, Cont. Add another key : value pair: emptyDict['age'] = 21 emptyDict is now {'name': 'Alan', 'age': 21} key : value pairs are separated by commas. Like sets, you cannot control the order of the key value pairs in the dict – the hashing algorithm controls this. However, since you cannot use index values, the position has no meaning. Winter 2016CISC101 - Prof. McLeod8

9 9 A List of Dictionaries – An Example >>> name1 = {'name':'Sam', 'age':18, 'SN':4445555} >>> name2 = {'name':'Boris', 'age':21, 'SN':5554444} >>> name3 = {'name':'Ben', 'age':19, 'SN':5445444} >>> allNames = [name1, name2, name3] >>> allNames [{'age': 18, 'name': 'Sam', 'SN': 4445555}, {'age': 21, 'name': 'Boris', 'SN': 5554444}, {'age': 19, 'name': 'Ben', 'SN': 5445444}] >>> allNames[2]['age'] 19 >>> allNames[1]['name'] 'Boris' Winter 2016

10 Better Yet: >>> allNames = {} >>> allNames['Sam'] = {'age':18, 'SN':4445555} >>> allNames['Boris'] = {'age':21, 'SN':5554444} >>> allNames['Ben'] = {'age':19, 'SN':5445444} >>> allNames {'Boris': {'age': 21, 'SN': 5554444}, 'Ben': {'age': 19, 'SN': 5445444}, 'Sam': {'age': 18, 'SN': 4445555}} >>> allNames['Boris']['age'] 21 CISC101 - Prof. McLeod10Winter 2016

11 CISC101 - Prof. McLeod11 Dictionaries, Cont. A dictionary has a method called keys() that returns an iterable list of key values: >>> name1.keys() dict_keys(['age', 'sex', 'name', 'SN']) If you apply the sorted() BIF to a dictionary it returns an iterable sorted by key to use with a for loop. See section 5.5 in the Python Tutorial. Winter 2016

12 For Example: Winter 2016CISC101 - Prof. McLeod12

13 Dictionary Methods See section 4.10 in the Python Standard Library: clear() – empties the dictionary fromkeys(keys, [values]) – returns a new dict get(key) – Obtains a key’s value, but returns None if the key is not in the dictionary. items() – returns a iterable of key, value tuples. keys() – returns an iterable of just keys pop(key) – remove key : value and return value. Winter 2016CISC101 - Prof. McLeod13

14 Dictionary Methods, Cont. popitem() – destructively iterates through a dictionary, returning key, value tuples. update(other) – updates existing values with values from other, which can be a dictionary or a collection of key, value tuples. values() – returns an iterable of just values. Winter 2016CISC101 - Prof. McLeod14

15 Another Example: Winter 2016CISC101 - Prof. McLeod15

16 Using Dictionaries These are particularly useful for field-based information, such as that stored in a database. It is easier to code using real field names rather than having to remember which fields are at which index locations. Winter 2016CISC101 - Prof. McLeod16

17 Dictionary Example See DictionaryDrawingProgram.py which reads the text file: Drawing.txt. Uses a list of dictionaries, with one dictionary per line in the drawing. Note how key names are much better than indices in identifying the elements of a collection. A list of dictionaries could be a good database structure! CISC101 - Prof. McLeod17Winter 2016

18 CISC101 - Prof. McLeod18 Lists of Lists We know a list can hold anything, and the elements do not even have to be of all the same type: ex1 = [1, 4.0, ‘abc’, 2, ‘hello!’] So, there is no reason that an element cannot be another list (or a tuple, or a dictionary). ex2 = [4.5, [1, 2, ‘abc’], 7, ‘hello’] Winter 2016

19 CISC101 - Prof. McLeod19 Lists of Lists, Cont. For example: >>> for value in ex2: print(value) 4.5 [1, 2, 'abc'] 7 hello Winter 2016

20 CISC101 - Prof. McLeod20 Lists of Lists, Cont. How can I display the elements in the list at position 1?: >>> for value in ex2[1]: print(value) 1 2 abc Winter 2016

21 CISC101 - Prof. McLeod21 Lists of Lists, Cont. Nothing new! How do I access just the 'abc' string inside the list at position 1?: >>> ex2[1][2] = 'wxyz' >>> ex2 [4.5, [1, 2, 'wxyz'], 7, 'hello'] Winter 2016

22 CISC101 - Prof. McLeod22 Lists of Lists, Cont. So, a list of lists can be used represent tabular data: ex3 = [['Sam', 18, 4445555], ['Boris', 21, 5554444], ['Ben', 19, 5445444]] You could do it this way, or (better yet) use a dictionary. Winter 2016 Sam184445555 Boris215554444 Ben195445444

23 Lists of Lists, Cont. Don’t forget that list locations are index based, so you can use the slice operator. List elements stay where you put them! So, while you cannot control the order of key : value pairs within a dictionary, you can control the position of dictionaries within a list. Winter 2016CISC101 - Prof. McLeod23


Download ppt "Today… Files from the Web! Dictionaries. Lists of lists. Winter 2016CISC101 - Prof. McLeod1."

Similar presentations


Ads by Google