Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
40 views

Text Data, Files, and Exceptions

Here are expressions to solve the problem: a) events.count('9/14') b) events.find('9/14') c) events.find('\n', events.find('9/14')+6) d) events[events.find('9/14'):events.find('\n', events.find('9/14')+6)].split('\n') This uses string methods like count(), find(), split() along with indexing to extract the relevant substrings and compute the requested values.

Uploaded by

riya
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views

Text Data, Files, and Exceptions

Here are expressions to solve the problem: a) events.count('9/14') b) events.find('9/14') c) events.find('\n', events.find('9/14')+6) d) events[events.find('9/14'):events.find('\n', events.find('9/14')+6)].split('\n') This uses string methods like count(), find(), split() along with indexing to extract the relevant substrings and compute the requested values.

Uploaded by

riya
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 42

Introduction to Computing Using Python

Text Data, Files, and Exceptions

§ Strings, revisited
§ Formatted output
§ File Input/Output
§ Errors and Exceptions

Original slides by Ljubomir Perkovic


Modified by Gene Moo Lee and Mi Zhou
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 (")
>>> excuse = 'I am sick'
What if ' or " is one of >>> excuse = "I am sick"
the string characters? >>> excuse = 'I'm sick'
SyntaxError: invalid syntax
What if the string includes >>> excuse = "I'm sick"
sick”
both ' and "? >>> excuse = "I'm "sick""
SyntaxError: invalid syntax
Escape sequence \' or \" is used to >>> excuse = 'I'm "sick"'
SyntaxError: invalid syntax
indicate that a quote is not the string >>> excuse = 'I\'m "sick"'
delimiter but is part of the string >>> excuse
value 'I\'m "sick"'
>>> print(excuse)
Function print() interprets the I'm "sick”
"sick"
>>> excuse = 'I\'m ...\n... "sick"'
escape sequence >>> excuse
'I\'m ...\n... "sick"'
Another example: >>> print(excuse)
• \n is an escape sequence that I'm ...
... "sick" excuse.py
represents a new line 2
Introduction to Computing Using Python

Indexing operator, revisited


The indexing operator returns
the character at index i (as a
single character string).

-5 -4 -3 -2 -1

s = 'A p p l e'
0 1 2 3 4

s[0] = 'A'
s[1] = 'p' >>>
>>>
s = 'Apple'
s[0]
'A'
s[2] = 'p' >>> s[1]
'p'
s[3] = 'l' >>> s[4]
'e'
s[4] = 'e'
3
Introduction to Computing Using Python

Indexing operator, revisited


The indexing operator can also s[i:j] : the slice of s starting at index i
be used to obtain a slice of a and ending before index j
string s[i:] : the slice of s starting at index i
s[:j] : the slice of s ending before index j
-5 -4 -3 -2 -1

s = 'A p p l e'
0 1 2 3 4
>>> s = 'Apple'
s[0:2] = 'A' p'
'A >>> s[0:2]
'Ap'
s[1:4] = 'p' p l'
'p >>> s[1:4]
>>> s = 'Apple'
>>> s[0]
'ppl'
'A' s[2:5]
s[2:5] = 'p' l e'
'p >>>
>>> s[1]
'ple'
'p' s[2:]
>>>
s[2:] = 'p 'l'
l e' >>> s[4]
'ple'
'e' s[:2]
>>>
s[:2] = 'A p' 'Ap'
>>> s[-3:-1] 4
s[-3:-1] = 'p l' 'pl'
Let’s practice!
• Jupyter Notebook:
Canvas –> Modules –> Text Data and File Input/Output

5
Introduction to Computing Using Python

Exercise (Notebook Problem 1)


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', 'c', 'd']


b) ['d', 'e', 'f']
c) ['d']
d) ['f', 'g']
e) ['d', 'e', 'f', 'g’, 'h']
f) ['f', 'g', 'h']

6
Introduction to Computing Using Python

String methods 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'
Strings are >>> link[20:25].capitalize()
'Smith'
immutable; none >>> link.replace('smith', 'ferreira')
of the string 'http://www.main.com/ferreira/index.html'
>>> link
methods modify 'http://www.main.com/smith/index.html'
string link >>> 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']
7
Introduction to Computing Using Python

String methods
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
Strings are s.find(target) returns the index of the first
occurrence of target in s
immutable; none
of the string s.lower() returns lowercase copy of s
methods modify s.replace(old, new) returns copy of s with every
string link 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 8
Let’s practice!
• Jupyter Notebook:
Canvas –> Modules –> Text Data and File Input/Output

9
Introduction to Computing Using Python

Exercise (Notebook Problem 2)


events.py
>>> events = '9/13 2:30 PM\n9/14 11:15 AM\n9/14 1:00 PM\n9/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 1st 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

10
Introduction to Computing Using Python

String methods
Suppose we need to pick up the date and time components of string event

>>> event = "Tuesday, Feb 29, 2012 -- 3:35 PM"


Punctuation makes >>> table = str.maketrans(':,-', 3*' ')
>>> event.translate(table)
it difficult to use 'Tuesday Feb 29 2012 3 35 PM'
method split() >>> event.translate(table).split()
['Tuesday', 'Feb', '29', '2012', '3', '35', 'PM']
>>>

events.py
Solution: replace punctuation with blank spaces

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 11
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.py

A blank space separator is printed between the arguments

12
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

13
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)

boa\n
boa
cat
cat\n
dog
dog\n
>>>

print.py

14
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)

boa\n
boa
cat\n
cat
dog\n
dog
>>> for pet in pets:
print(pet, end=', ')

boa, cat, dog,


>>> for pet in pets:
print(pet, end='!!! ')

boa!!! cat!!! dog!!! print.py


>>>

The end argument allows for customized end characters 15


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.py

and we want to print Wednesday, March 10, 2010 at 11:45:3316


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)
TypeError: unsupported operand type(s) for +: 'int' and 'str'
>>> print(str(hour)+':'+str(minute)+':'+str(second))
11:45:33
>>> print('{}:{}:{}'.format(hour, minute, second))
11:45:33 print.py

and we want to print Wednesday, March 10, 2010 at 11:45:3317


Introduction to Computing Using Python

Method format() of class str


>>> day = 'Wednesday'
>>> month = 'March'
>>> weekday = 'Wednesday'
>>> month = 'March'
>>> day = 10
>>> year = 2010
>>> 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 18
Introduction to Computing Using Python

Method format() of class str


>>> day = 'Wednesday'
>>> month = 'March'
>>> weekday = 'Wednesday'
>>> month = 'March'
>>> day = 10
>>> year = 2010
>>> 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

print('{}:{}:{}'.format(hour, minute, second))

19
Introduction to Computing Using Python

Specifying field width


print.py
>>> for i in range(1,8):
The format() method >>> for iprint(i,
in range(1,8):
i**2, 2**i)
print(i, i**2, 2**i)
can be used to line up
data in columns 1 1 2
2
1 4
1 4
2
2 9
3 4 8
4
3 16
4 9 816
4 25
5 16 32
16
5 36
6 25 64
32
7
6 49
36 128
64
7 49for
>>> 128i in range(1, 8):
>>> print('{} {:2} {:3}'.format(i, i**2,
2**i))

1 1 2 reserves 2 spaces for i**2


2 4 4
3 9 8
4 16 16
5 25 32
6 36 64 20
7 49 128
Introduction to Computing Using Python

Specifying field width


print.py
>>> for i in range(1,8):
The format() method >>> for iprint(i,
in range(1,8):
i**2, 2**i)
print(i, i**2, 2**i)
can be used to line up
data in columns 1 1 2
2
1 4
1 4
2
2 9
3 4 8
4
3 16
4 9 816
4 25
5 16 32
16
5 36
6 25 64
32
7
6 49
36 128
64
7 49for
>>> 128i in range(1, 8):
>>> print('{} {:2} {:3}'.format(i, i**2,
2**i))

1 1 2
2 4 4
3 9 8
4 16 16 reserves 3 spaces for 2**i
5 25 32
6 36 64 21
7 49 128
Introduction to Computing Using Python

Specifying field width


print.py
>>> for i in range(1,8):
The format() method >>> for iprint(i,
in range(1,8):
i**2, 2**i)
print(i, i**2, 2**i)
can be used to line up
data in columns 1 1 2
2
1 4
1 4
2
2 9
3 4 8
4
3 16
4 9 816
4 25
5 16 32
16
Numbers are aligned to 5 36
6 25 64
32
the right 7
6 49
36 128
64
7 49for
>>> 128i in range(1, 8):
>>> print('{} {:2} {:3}'.format(i, i**2,
2**i))

1 1 2 plus a blank space between the columns


2 4 4
3 9 8
4 16 16
5 25 32
6 36 64 22
7 49 128
Introduction to Computing Using Python

Specifying field width

The format() method


can be used to line up >>> lst = ['Alan Turing', 'Ken Thompson', 'Vint Cerf']
data in columns >>> for name in lst:
fl = name.split()
print(fl[0], fl[1])

Numbers are aligned to Alan Turing


the right Ken Thompson
Vint Cerf
>>> for name in lst:
fl = name.split()
Strings are aligned to the print('{:5} {:10}'.format(fl[0], fl[1]))
left
Alan Turing
Ken Thompson
Vint Cerf print.py
>>>
23
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

>>> n = 10
>>> '{:b}'.format(n)
'1010'
Type Explanation >>> '{:c}'.format(n)
'{:7.2f}'
b binary '\n'

c character
>>> '{:d}'.format(n)
'10'
field width
d decimal
>>> '{:X}'.format(n) decimal precision
'A'
X hexadecimal >>> '{:e}'.format(n)
'1.000000e+01'
e scientific >>> '{:7.2f}'.format(n)
' 10.00'
f fixed-point >>>

print.py 24
Introduction to Computing Using Python

Files and the file system


root folder
/ The file system is the OS component
that organizes files and provides a way
to create, access, and modify files
Applications bin Users var

Mail.app Firefox.app messi Shared poem.txt


Files are organized into a tree structure
Contents image.jpg poem.txt Canon
• folders (or directories)
MacOS • regular files
binary file text file
While every file and folder has a name, it is
Mail
the file pathname that identifies the file
Absolute pathnames
• /var/poem.txt
• /Users/messi/poem.txt
• /Applications/Mail.app/ 25
Introduction to Computing Using Python

Files and the file system


root folder
/ The file system is the OS component
that organizes files and provides a way
to create, access, and modify files
Applications bin Users var

Mail.app Firefox.app messi Shared poem.txt


Files are organized into a tree structure
Contents image.jpg poem.txt Canon
• folders (or directories)
MacOS • regular files
binary file text file
While every file and folder has a name, it is
Mail
the file pathname that identifies the file
Relative pathnames (relative to current working directory Users)
• messi/poem.txt
• messi/image.jpg
• Shared 26
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 File mode 'r' is used to
3. Closing the file open a file for reading
(rather than, say, writing)
Built-in function open() is used to open a file
• 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
>>> infile = open('sample.txt')
Traceback (most recent call last):
File "<pyshell#50>", line 1, in <module>
infile = open('sample.txt')
A “file” object is of a type that IOError: [Errno 2] No such file or directory:
supports several “file” methods, 'sample.txt'
>>> infile = open('example.txt', 'r')
including method close() that >>> infile.close()
closes the file >>> 27
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

>>> infile = open('example.txt', 'rt')


>>> infile = open('example.txt', 'r')
These are all equivalent >>> infile = open('example.txt', 't')
>>> infile = open('example.txt')
28
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(n) Close file infile 29
Introduction to Computing Using Python

Reading a file

1 The 3 lines in this file end with the new line character.\n
2 \n
3 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
30
Introduction to Computing Using Python

Reading a file

1 The 3 lines in this file end with the new line character.\n
2 \n
3 There is a blank line above this line.\n
example.txt

When the file is opened, a cursor is associated with the opened file

>>> infile = open('example.txt')


>>> infile.read(1)
The initial position of the 'T'
cursor is: >>> infile.read(5)
'he 3 '
• at the beginning of the >>> infile.readline()
file, if file mode is r 'lines in this file end with the new line
character.\n'
>>> infile.read()
• at the end of the file, if '\nThere is a blank line above this line.\n'
file mode is a or w >>> infile.close()
>>>
31
Let’s practice!
• Jupyter Notebook:
Canvas –> Modules –> Text Data and File Input/Output

32
Introduction to Computing Using Python

Exercise (Notebook Problem 3):


Patterns for reading a text file

Please explain the purpose of each function and add appropriate docstring

Example:

def f1(filename):
'returns ...'

infile = open(filename, 'r')


content = infile.read()
infile.close()

return len(content)

ch4.py
33
Introduction to Computing Using Python

Exercise (Notebook Problem 3):


Patterns for reading a text file

Please explain the purpose of each function and add appropriate docstring

Example:

def f2(filename):
'returns ...'

infile = open(filename)
content = infile.read()
infile.close()
wordList = content.split()

return len(wordList)

ch4.py
34
Introduction to Computing Using Python

Exercise (Notebook Problem 3):


Patterns for reading a text file

Please explain the purpose of each function and add appropriate docstring

Example:

def f3(filename):
'returns ...'

infile = open(filename, 'r’)


lineList = infile.readlines()
infile.close()

return len(lineList)

ch4.py
35
Introduction to Computing Using Python

Writing to a text file

1 T
This is the first line. Still the first line…\n
2 Now we are in the second line.\n
3 Non string value like 5 must be converted first.\n
4 Non string value like 5 must be converted first.\n
test.txt

>>> outfile = open('test.txt', 'w')


>>> outfile.write('T')
write_test.py
1
>>> outfile.write('his is the first line.')
22
>>> outfile.write(' Still the first line...\n')
25
>>> outfile.write('Now we are in the second line.\n')
31
>>> outfile.write('Non string value like '+str(5)+' must be converted first.\n')
49
>>> outfile.write('Non string value like {} must be converted first.\n'.format(5))
49 36
>>> outfile.close()
Introduction to Computing Using Python

Types of errors

We saw different types of errors in this chapter

>>> excuse = 'I'm sick'


SyntaxError: invalid syntax
>>> print(hour+':'+minute+':'+second)
Traceback (most recent call last):
File "<pyshell#113>", line 1, in <module>
print(hour+':'+minute+':'+second)
TypeError: unsupported operand type(s) for +:
'int' and 'str’
There are basically two >>> infile = open('sample.txt')
Traceback (most recent call last):
types of errors: File "<pyshell#50>", line 1, in <module>
• syntax errors infile = open('sample.txt')
IOError: [Errno 2] No such file or directory:
• erroneous state 'sample.txt’
errors

37
Introduction to Computing Using Python

Syntax errors
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]
SyntaxError: invalid syntax
>>> if x == 5
SyntaxError: invalid syntax
>>> print 'hello'
SyntaxError: invalid syntax
>>> lst = [4;5;6]
SyntaxError: invalid syntax
>>> for i in range(10):
print(i)
SyntaxError: expected an indented block

38
Introduction to Computing Using Python

Erroneous state errors

The program execution gets into an erroneous state

>>> 3/0
Traceback (most recent call last):
File "<pyshell#56>", line 1, in <module>
3/0
ZeroDivisionError: division by zero

39
Introduction to Computing Using Python

Erroneous state errors

The program execution gets into an erroneous state


>>> lst
Traceback (most recent call last):
File "<pyshell#57>", line 1, in <module>
lst
NameError: name 'lst' is not defined

>>> lst = [12, 13, 14]


>>> lst[3]
Traceback (most recent call last):
File "<pyshell#59>", line 1, in <module>
lst[3]
IndexError: list index out of range

>>> lst * lst


Traceback (most recent call last):
File "<pyshell#60>", line 1, in <module>
lst * lst
TypeError: can't multiply sequence by non-int
of type 'list’
40
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

• This object has a type that is related to the type of error


• The object contains information about the error
• The default behavior is to print this information and interrupt the execution of
the statement.

The “error” object is called an exception; the creation of an exception due to


an error is called the raising of an exception

>>> int('4.5')
Traceback (most recent call last):
File "<pyshell#61>", line 1, in <module>
int('4.5')
ValueError: invalid literal for int() with
base 10: '4.5'
41
Introduction to Computing Using Python

Exception types

Some of the built-in exception classes:

Exception Explanation
KeyboardInterrupt Raised when user hits Ctrl-C, the interrupt key
OverflowError Raised when a floating-point expression evaluates to a value that is
too large
ZeroDivisionError Raised when attempting to divide by 0
IOError Raised when an I/O operation fails for an I/O-related reason
IndexError Raised when a sequence index is outside the range of valid indexes
NameError Raised when attempting to evaluate an unassigned identifier (name)
TypeError Raised when an operation of function is applied to an object of the
wrong type
ValueError Raised when operation or function has an argument of the right
42
type
but incorrect value

You might also like