diff --git a/generators.html b/generators.html index 9e5bb5f..afcff1e 100755 --- a/generators.html +++ b/generators.html @@ -274,15 +274,15 @@ $ $ s return [matches_rule, apply_rule] rules = [] -with open('plural4-rules.txt') as pattern_file: ② - for line in pattern_file: ③ - pattern, search, replace = line.split(None, 3) ④ - rules.append(build_match_and_apply_functions( ⑤ +with open('plural4-rules.txt', encoding='utf-8') as pattern_file: ② + for line in pattern_file: ③ + pattern, search, replace = line.split(None, 3) ④ + rules.append(build_match_and_apply_functions( ⑤ pattern, search, replace))
build_match_and_apply_functions() function has not changed. You’re still using closures to build two functions dynamically that use variables defined in the outer function.
-open() function opens a file and returns a file object. In this case, the file we’re opening contains the pattern strings for pluralizing nouns. The with statement creates what’s called a context: when the with block ends, Python will automatically close the file, even if an exception is raised inside the with block. You’ll learn more about with blocks in the Files chapter.
-for line in <fileobject> idiom reads data from the open file, one line at a time, and assigns the text to the line variable. A “line” of a text file is just what you think it is — a sequence of characters delimited by a carriage return. Of course, it can’t really be that simple, can it? Text files can use several different characters to mark the end of a line. Some use a carriage return character, others use a line feed character, and some use both characters at the end of every line. Python handles all of these cases automatically, so you can say, “Hey, I want to read this text file one line at a time” and it will Just Work. You’ll learn more about reading from and writing to in the Files chapter.
+open() function opens a file and returns a file object. In this case, the file we’re opening contains the pattern strings for pluralizing nouns. The with statement creates what’s called a context: when the with block ends, Python will automatically close the file, even if an exception is raised inside the with block. You’ll learn more about with blocks and file objects in the Files chapter.
+for line in <fileobject> idiom reads data from the open file, one line at a time, and assigns the text to the line variable. You’ll learn more about reading from and writing to in the Files chapter.
split() string method. The first argument to the split() method is None, which means “split on any whitespace (tabs or spaces, it makes no difference).” The second argument is 3, which means “split on whitespace 3 times, then discard the rest of the line.” A line like [sxz]$ $ es will be broken up into the list ['[sxz]$', '$', 'es'], which means that pattern will get '[sxz]$', search will get '$', and replace will get 'es'. That’s a lot of power in one little line of code.