Introduction to Computing Using Python Text Data File


![Introduction to Computing Using Python Indexing operator, revisited s[i: j] : the slice of Introduction to Computing Using Python Indexing operator, revisited s[i: j] : the slice of](https://slidetodoc.com/presentation_image_h2/449758a3f0a3d856fd9e4151282c7632/image-3.jpg)




























- Slides: 31

Introduction to Computing Using Python Text Data, File I/O, and Exceptions § § Strings, revisited Formatted output File Input/Output Errors and Exceptions Original slides by Ljubomir Perkovic Modified by Gene Moo Lee 1

Introduction to Computing Using Python String representations A string value is represented as a sequence of characters delimited by quotes Quotes can be single (') or double (") ' orstring " is one of What if the includes both ' andcharacters? "? the string Escape sequence ' or " is used to indicate that a quote is not the string delimiter but is part of the string value Function print() interprets the escape sequence Another example: • n is an escape sequence that represents a new line >>> excuse = 'I am sick' >>> excuse = "I am sick" >>> excuse = 'I'm sick' Syntax. Error: invalid syntax >>> excuse = "I'm sick" sick” >>> excuse = "I'm "sick"" Syntax. Error: invalid syntax >>> excuse = 'I'm "sick"' Syntax. Error: invalid syntax >>> excuse = 'I'm "sick"' >>> excuse 'I'm "sick"' >>> print(excuse) I'm "sick” "sick" >>> excuse = 'I'm. . . n. . . "sick"' >>> excuse 'I'm. . . n. . . "sick"' >>> print(excuse) I'm. . . excuse. py. . . "sick" 2
![Introduction to Computing Using Python Indexing operator revisited si j the slice of Introduction to Computing Using Python Indexing operator, revisited s[i: j] : the slice of](https://slidetodoc.com/presentation_image_h2/449758a3f0a3d856fd9e4151282c7632/image-3.jpg)
Introduction to Computing Using Python Indexing operator, revisited s[i: j] : the slice of s starting at index i The indexing operator can returns also theused be character to obtain at index a slice i (as of aa single character string). string and ending before index j s[i: ] : the slice of s starting at index i s[: j] : the slice of s ending before index j -5 s = 'A 0 -4 -3 -2 -1 p p l e' 1 2 3 4 'A' p' 'A 'p' p s[1] s[1: 4] = 'p l' 'p' l s[2] s[2: 5] = 'p e' s[3] s[2: ] = 'p 'l' l e' 'e' s[: 2] = 'A s[4] p' s[-3: -1] = 'p l' s[0] s[0: 2] = >>> s = 'Apple' >>> s[0: 2] 'Ap' >>> s[1: 4] s = 'Apple' >>> s[0] 'ppl' 'A' s[2: 5] >>> s[1] 'ple' 'p' s[2: ] >>> s[4] 'ple' 'e' s[: 2] >>> 'Ap' >>> s[-3: -1] 'pl' 3

Introduction to Computing Using Python Exercise (Notebook) The indexing operator can also be used to obtain slices of a list as well. Let list lst refer to list ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h’] Write Python expressions using list lst and the indexing operator that evaluate to: a) ['a', b) ['d', c) ['d'] d) ['f', e) ['d', f) ['f', 'b', 'c', 'd'] 'e', 'f'] 'g'] 'e', 'f', 'g’, 'h'] 'g', 'h'] >>> lst[: 4] ['a', 'b', 'c', 'd'] >>> lst[3: 6] ['d', 'e', 'f'] >>> lst[3: 4] ['d'] >>> lst[-3: -1] ['f', 'g'] >>> lst[3: ] ['d', 'e', 'f', 'g’, 'h'] >>> lst[-3: ] ['f', 'g', 'h'] 4

Introduction to Computing Using Python String methods Strings are immutable; none of the string methods modify string link. py >>> link = 'http: //www. main. com/smith/index. html' >>> link[: 4] 'http' >>> link[: 4]. upper() 'HTTP' >>> link. find('smith') 20 >>> link[20: 25] 'smith' >>> link[20: 25]. capitalize() 'Smith' >>> link. replace('smith', 'ferreira') 'http: //www. main. com/ferreira/index. html' >>> link 'http: //www. main. com/smith/index. html' >>> new = link. replace('smith', 'ferreira') >>> new 'http: //www. main. com/ferreira/index. html' >>> link. count('/') 4 >>> link. split('/') ['http: ', 'www. main. com', 'smith', 'index. html'] 5

Introduction to Computing Using Python String methods Strings are immutable; none of the string methods modify string link Usage Explanation s. capitalize() returns a copy of s with first character capitalized s. count(target) returns the number of occurences of target in s s. find(target) returns the index of the first occurrence of target in s s. lower() returns lowercase copy of s s. replace(old, new) returns copy of s with every occurrence of old replaced with new s. split(sep) returns list of substrings of s, delimited by sep s. strip() returns copy of s without leading and trailing whitespace s. upper() returns uppercase copy of s 6

Introduction to Computing Using Python Exercise (Notebook) events. py >>> events = '9/13 2: 30 PMn 9/14 11: 15 AMn 9/14 1: 00 PMn 9/15 9: 00 AM' >>> print(events) 9/13 2: 30 PM 9/14 11: 15 AM 9/14 1: 00 PM 9/15 9: 00 AM String events describes the schedule of 4 events spread across 3 days Write expressions that compute: a) the number of events on 9/14 b) the index of the substring describing the 1 st event on 9/14 c) the index just past the substring describing the last event on 9/14 d) the list of substrings describing the events on 9/14 >>> events. count('9/14') 2 >>> events. find('9/14') 13 >>> events. find('9/15') 40 >>> events[13: 40] '9/14 11: 15 AMn 9/14 1: 00 PMn’ >>> lst = events[13: 40]. strip(). split('n') >>> lst ['9/14 11: 15 AM', '9/14 1: 00 PM'] >>> 7

Introduction to Computing Using Python String methods Suppose we need to pick up the date and time components of string event Puntuation makes it difficult to use method split() >>> event = "Tuesday, Feb 29, 2012 -- 3: 35 PM" >>> table = str. maketrans(': , -', string. maketrans(': , -', 3*'3*' ') ') >>> event. translate(table) 'Tuesday Feb 29 2012 3 35 PM' >>> event. translate(table). split() ['Tuesday', 'Feb', '29', '2012', '35', 'PM'] >>> Solution: replace punctuation with blank spaces events. py Usage Explanation str. maketrans(old, new) returns a table mapping characters in string old to characters in string new s. translate(table) returns a copy of s in which the original characters are replaced using the mapping described by table 8

Introduction to Computing Using Python Built-in function print(), revisited their string representation Function print() takes 0 or more arguments and prints them in the shell >>> prod = 'morels' >>> cost = 139 >>> wght = 1/2 >>> total = cost * wght >>> print(prod, cost, wght, total) morels 139 0. 5 69. 5 >>> print(prod, cost, wght, total, sep='; ') morels; 139; 0. 5; 69. 5 >>> print(prod, cost, wght, total, sep=': : : ') morels: : : 139: : : 0. 5: : : 69. 5 >>> print. py A blank space separator is printed between the arguments The sep argument allows for customized separators 9

Introduction to Computing Using Python Built-in function print(), revisited Function print() prints, by default, a newline character after printing its arguments >>> pets = ['boa', 'cat', 'dog'] >>> for pet in pets: print(pet) boan boa catn cat dogn dog >>> for pet in pets: print(pet, end=', ') boa, cat, dog, >>> for pet in pets: print(pet, end='!!! ') boa!!! cat!!! dog!!! >>> The end argument allows for customized end characters print. py 10

Introduction to Computing Using Python General output formatting Suppose we have >>> weekday = 'Wednesday' >>> month = 'March' >>> day = 10 >>> year = 2010 >>> hour = 11 >>> minute = 45 >>> second = 33 >>> print(hour+': '+minute+': '+second) Traceback (most recent call last): File "<pyshell#113>", line 1, in <module> print(hour+': '+minute+': '+second) Type. Error: unsupported operand type(s) for +: 'int' and 'str' >>> print(str(hour)+': '+str(minute)+': '+str(second)) 11: 45: 33 >>> print('{}: {}'. format(hour, minute, second)) print. py 11: 45: 33 and we want to print Wednesday, March 10, 2010 at 11: 45: 3311

Introduction to Computing Using Python Method format() of class str >>> day = 'Wednesday' >>> month = 'March' >>> weekday = 'Wednesday' >>> month = 'March' >>> day = 10 >>> year = 2012 >>> hour = 11 >>> minute = 45 >>> second = 33 >>> print('{}: {}'. format(hour, minute, second)) 11: 45: 33 >>> print('{}, {} at {}: {}'. format(weekday, month, day, year, hour, minute, second)) Wednesday, March 10, 2012 at 11: 45: 33 print. py format string print('{}: {}'. format(hour, minute, second)) placeholders 12

Introduction to Computing Using Python Specifying field width print. py The format() method can be used to line up data in columns Numbers are aligned to the right >>> for i in range(1, 8): >>> for iprint(i, in range(1, 8): i**2, 2**i) print(i, i**2, 2**i) 1 1 2 1 4 2 2 2 9 4 8 4 3 3 16 9 816 4 4 25 16 32 16 5 5 36 25 64 32 6 6 49 36 128 64 7 7 49 for 128 i in range(1, 8): >>> print('{} {: 2} {: 3}'. format(i, i**2, 2**i)) 1 2 3 4 5 6 7 1 2 4 4 9 8 16 16 25 32 36 64 49 128 reserves spacesbetween for i**2 the columns plus a blank 2 space reserves 3 spaces for 2**i 13

Introduction to Computing Using Python Specifying field width The format() method can be used to line up data in columns Numbers are aligned to the right Strings are aligned to the left >>> lst = ['Alan Turing', 'Ken Thompson', 'Vint Cerf'] >>> for name in lst: fl = name. split() print(fl[0], fl[1]) Alan Turing Ken Thompson Vint Cerf >>> for name in lst: fl = name. split() print('{: 5} {: 10}'. format(fl[0], fl[1])) Alan Ken Vint >>> Turing Thompson Cerf print. py 14

Introduction to Computing Using Python Output format type Inside the curly braces of a placeholder, we can specify the field width, width the type of the output, output and the decimal precision Type Explanation b binary c character d decimal X hexadecimal e scientific f fixed-point >>> n = 10 >>> '{: b}'. format(n) '1010' >>> '{: c}'. format(n) 'n' >>> '{: d}'. format(n) '10' >>> '{: X}'. format(n) 'A' >>> '{: e}'. format(n) '1. 000000 e+01' >>> '{: 7. 2 f}'. format(n) ' 10. 00' >>> print. py '{: 7. 2 f}' field width decimal precision 15

Introduction to Computing Using Python Text Data, File I/O, and Exceptions § § Strings, revisited Formatted ouput File Input/Output Errors and Exceptions Original slides by Ljubomir Perkovic Modified by Gene Moo Lee 16

Introduction to Computing Using Python Files and the file system root folder / Applications bin Mail. app Firefox. app Contents image. jpg Mac. OS Mail binary file Users messi The file system is the OS component that organizes files and provides a way to create, access, and modify files var Shared poem. txt text file poem. txt Canon Files are organized into a tree structure • folders (or directories) • regular files While every file and folder has a name, it is the file pathname that identifies the file Absolute pathnames. Relative pathnames (relative to current working directory Users) • /var/poem. txt • messi/poem. txt • /Users/messi/poem. txt • messi/image. jpg • /Applications/Mail. app/ • Shared 17

Introduction to Computing Using Python Opening and closing a file Processing a file consists of: 1. Opening the file 2. Reading from and/or writing to the file 3. Closing the file Built-in function open() is used to open a file File mode 'r' is used to open a file for reading (rather than, say, writing) • The first input argument is the file pathname, whether absolute or relative with respect to the current working directory • The second (optional) argument is the file mode • Returns a “file” object A “file” object is of a type that supports several “file” methods, including method close() that closes the file >>> infile = open('sample. txt') Traceback (most recent call last): File "<pyshell#50>", line 1, in <module> infile = open('sample. txt') IOError: [Errno 2] No such file or directory: 'sample. txt' >>> infile = open('example. txt', 'r') >>> infile. close() >>> 18

Introduction to Computing Using Python Open file mode The file mode defines how the file will be accessed Mode Description r Reading (default) w Writing (if file exists, content is wiped) a Append (if file exists, writes are appended) r+ Reading and Writing t Text (default) b Binary These are all equivalent >>> >>> infile = = open('example. txt', 'rt') open('example. txt', 'r') open('example. txt', 't') open('example. txt') 19

Introduction to Computing Using Python File methods There are several “file” types; they all support similar “file” methods • Methods read() and readline() return the characters read as a string • Methods readlines() returns the characters read as a list of lines • Method write() returns the number of characters written Usage Description infile. read(n) Read n characters starting from cursor; if fewer than n characters remain, read until the end of file infile. read() Read starting from cursor up to the end of the file infile. readline() Read starting from cursor up to, and including, the end of line character infile. readlines() Read starting from cursor up to the end of the file and return list of lines outfile. write(s) Write string s to file outfile starting from cursor infile. close() Close file infile 20

Introduction to Computing Using Python Reading a file 1 2 3 The 3 lines in this file end with the new line character. n �� n � � There is a blank line above this line. n � example. txt When the file is opened, a cursor is associated with the opened file The initial position of the cursor is: • at the beginning of the file, if file mode is r • at the end of the file, if file mode is a or w >>> infile = open('example. txt') >>> infile. read(1) 'T' >>> infile. read(5) 'he 3 ' >>> infile. readline() 'lines in this file end with the new line character. n' >>> infile. read() 'n. There is a blank line above this line. n' >>> infile. close() >>> 21

Introduction to Computing Using Python Exercise: Patterns for reading a text file Common patterns for reading a file: 1. Read the file content into a string 2. Read the file content into a list of words 3. Read the file content into a list of lines Example: def num. Chars(filename): 'returns the number of characters in filename' infile = open(filename, 'r') content = infile. read() infile. close() return len(content) ch 4. py 22

Introduction to Computing Using Python Patterns for reading a text file Common patterns for reading a file: 1. Read the file content into a string 2. Read the file content into a list of words 3. Read the file content into a list of lines Example: def num. Words(filename): 'returns the number of words in filename' infile = open(filename) content = infile. read() infile. close() word. List = content. split() return len(word. List) ch 4. py 23

Introduction to Computing Using Python Patterns for reading a text file Common patterns for reading a file: 1. Read the file content into a string 2. Read the file content into a list of words 3. Read the file content into a list of lines Example: def num. Lines(filename): 'returns the number of lines in filename' infile = open(filename, 'r’) line. List = infile. readlines() infile. close() return len(line. List) ch 4. py 24

Introduction to Computing Using Python Writing to a text file 1 This is the first line. Still the first line…n T � � � line. n 2 Now we are in the second � 3 Non string value like 5 must be converted first. n 4� Non string value like 5 must be converted first. n � >>> 1 >>> 22 >>> 25 >>> 31 >>> 49 >>> outfile = open('test. txt', 'w') outfile. write('T') � test. txt write_test. py outfile. write('his is the first line. ') outfile. write(' Still the first line. . . n') outfile. write('Now we are in the second line. n') outfile. write('Non string value like '+str(5)+' must be converted first. n') outfile. write('Non string value like {} must be converted first. n'. format(5)) outfile. close() 25

Introduction to Computing Using Python Text Data, File I/O, and Exceptions § § Strings, revisited Formatted ouput File Input/Output Errors and Exceptions Original slides by Ljubomir Perkovic Modified by Gene Moo Lee 26

Introduction to Computing Using Python Types of errors We saw different types of errors in this chapter There are basically two types of errors: • syntax errors • erroneous state errors >>> excuse = 'I'm sick' Syntax. Error: invalid syntax >>> print(hour+': '+minute+': '+second) Traceback (most recent call last): File "<pyshell#113>", line 1, in <module> print(hour+': '+minute+': '+second) Type. Error: unsupported operand type(s) for +: 'int' and 'str’ >>> infile = open('sample. txt') Traceback (most recent call last): File "<pyshell#50>", line 1, in <module> infile = open('sample. txt') IOError: [Errno 2] No such file or directory: 'sample. txt’ 27

Introduction to Computing Using Python Syntax errors are errors that are due to the incorrect format of a Python statement • They occur while the statement is being translated to machine language and before it is being executed. >>> (3+4] Syntax. Error: invalid syntax >>> if x == 5 Syntax. Error: invalid syntax >>> print 'hello' Syntax. Error: invalid syntax >>> lst = [4; 5; 6] Syntax. Error: invalid syntax >>> for i in range(10): print(i) Syntax. Error: expected an indented block 28

Introduction to Computing Using Python Erroneous state errors The program execution gets into an erroneous state When an error occurs, an “error” object is created 3/0 to the type of error • This object has a type that is>>> related Traceback (most recent call last): • The object contains information about the error File "<pyshell#56>", line 1, in <module> • The default behavior is to print >>>this lst 3/0 information and interrupt the execution of Traceback (most recent call last): the statement. Zero. Division. Error: division by zero File "<pyshell#57>", line 1, in <module> >>> lst = [12, 13, 14] >>> lst[3] name 'lst' is not defined Name. Error: The “error” object is called an exception; the creation of an exception due to Traceback (most recent call last): an error is called the raising of an exception File line 1, in <module> >>> lst"<pyshell#59>", * lst[3](most recent call last): Traceback Index. Error: list index out File "<pyshell#60>", lineof 1, range in <module> >>> int('4. 5') lst * lst Traceback call last): by non-int Type. Error: (most can'trecent multiply sequence line 1, in <module> of. File type"<pyshell#61>", 'list’ int('4. 5') Value. Error: invalid literal for int() with base 10: '4. 5' 29

Introduction to Computing Using Python Exception types Some of the built-in exception classes: Exception Explanation Keyboard. Interrupt Raised when user hits Ctrl-C, the interrupt key Overflow. Error Raised when a floating-point expression evaluates to a value that is too large Zero. Division. Error Raised when attempting to divide by 0 IOError Raised when an I/O operation fails for an I/O-related reason Index. Error Raised when a sequence index is outside the range of valid indexes Name. Error Raised when attempting to evaluate an unassigned identifier (name) Type. Error Raised when an operation of function is applied to an object of the wrong type Value. Error Raised when operation or function has an argument of the right type 30 but incorrect value

Introduction to Computing Using Python We covered Text Data, File I/O, and Exceptions § § Strings, revisited Formatted ouput File Input/Output Errors and Exceptions Original slides by Ljubomir Perkovic Modified by Gene Moo Lee 31