解析Data Focused Python、Python语言程序辅导、辅导讲解Python编程留学生

- 首页 >> Python编程

Data Focused Python, 2018 K1

Homework 5


Due At 11:59 pm Monday, Oct. 1


1.(100  points) Regular Expressions


The expenses.txt file provides good examples for regular expression matching.


a.Create a Python script file named expense_regex.py.  In this script, define an empty list named records, then read the lines from expenses.txt and append each line (excluding its terminating newline character) to the records list.


Add this code for displaying lines from records that match a trivial regular expression:


import re


# 1a

pat = r'D'


for line in records:

   if re.search(pat, line) != None:

       print(line)


Confirm that the output is lines from records that contain a D somewhere.


b.Comment out the pattern for part 1a, and add a new pattern for part 1b, like this:


# 1a

# pat = r'D'


# 1b

pat = r'\''


Confirm that the output is lines from records that contain a single quote (') character

somewhere.


c.Comment out the pattern for part 1b, and add a new pattern for part 1c that will display lines from records that contain a double quote (") character somewhere.  Run the module to test.


d.Continue in this manner of commenting out the previous part’s pattern and adding a new pattern.  Add a pattern that will display lines that begin with 7; run the module to test.


e.Add a pattern that will display lines that end with an r or a t; test.



f.Add a pattern that will display lines that contain a literal period (.) character; test.


g.Display lines that contain an r followed later by a g.  (The r and the g do not need to be consecutive characters.)


h.Display lines that contain two consecutive uppercase letters (for example, AA, DF, LM, YW, …).


i.Display lines that contain a comma (,) character.


j.Display lines that contain three or more comma characters (not necessarily consecutive).


k.Display lines that do not contain any v, w, x, y, or z characters.


l.Display lines that contain money amounts between 10.00 and 99.99.  Hint: What must the first character be?  What must the second character be?  What must the third character be?  And so forth.  The pattern '[10.00-99.99]' will not work, because one pair of square brackets only matches a single character, not a range of multiple characters.  For example, '[aaaaaaa]' matches one a, not a sequence of seven a characters.  The pattern '[abcabcabcaaabbbaaaccccccc]' matches the same thing as the pattern '[abc]' or the pattern '[acb]' or '[bac]' and so forth: that is, either an a or a b or a c.  The regular expression pattern '[AA-ZZ]' matches either an A, or a character in the range from A through Z, or a Z; that is, the pattern is equivalent to '[A-Z]'.


m.Display lines that contain exactly three commas.


n.Display lines that contain a ( character (that is, an open parenthesis character).


o.Display lines that describe meals costing at least 100.00.


p.Display lines that have an expense category that is exactly four characters wide (your pattern should work even if more lines are added to the file, with new categories that have not yet been defined).


q.Display lines for expenses that occurred in March.


r.Display lines that contain an a, followed by a b, followed by a c (perhaps with other characters between the a and the b and the c).


s.Display lines that contain some sequence of two characters, followed later by that same sequence of two characters, followed later by that same sequence of two characters again.  That is, each matched line should contain at least one sequence of two characters at least three times.



t.Display lines in which the expense description contains a both a lowercase a and a decimal digit character between 0 and 9, in either order.  That is, the a might appear before the digit, or the a might appear after the digit.


u.Display lines that contain no uppercase letters.


v.Display lines that contain a d character, possibly followed by one optional character, followed by an i character.  (Matches would include words like diver, doily, drip, diplomat, etc.)




REMEMBER to put both authors’ names into your source code file.  Put your expense_regex.py file into a .zip archive and upload to the course web site.


站长地图