From e35d9d1bda97411ab770091c17f5e5d4aab8c39d Mon Sep 17 00:00:00 2001 From: Mark Pilgrim Date: Thu, 16 Jul 2009 12:36:37 -0400 Subject: [PATCH] add encoding parameter to all file open() calls in code samples, example files, and text --- advanced-iterators.html | 8 ++++---- diveintopython3.org | 1 + examples/plural4.py | 2 +- examples/plural5.py | 2 +- examples/plural6.py | 2 +- files.html | 20 ++++++++++---------- generators.html | 4 ++-- iterators.html | 8 ++++---- 8 files changed, 24 insertions(+), 23 deletions(-) diff --git a/advanced-iterators.html b/advanced-iterators.html index 43d7dc6..da407d7 100755 --- a/advanced-iterators.html +++ b/advanced-iterators.html @@ -309,19 +309,19 @@ StopIteration

[download favorite-people.txt]

->>> names = list(open('examples/favorite-people.txt'))  
+>>> names = list(open('examples/favorite-people.txt', encoding='utf-8'))  
 >>> names
 ['Dora\n', 'Ethan\n', 'Wesley\n', 'John\n', 'Anne\n',
 'Mike\n', 'Chris\n', 'Sarah\n', 'Alex\n', 'Lizzie\n']
->>> names = [name.rstrip() for name in names]           
+>>> names = [name.rstrip() for name in names]                             
 >>> names
 ['Dora', 'Ethan', 'Wesley', 'John', 'Anne',
 'Mike', 'Chris', 'Sarah', 'Alex', 'Lizzie']
->>> names = sorted(names)                               
+>>> names = sorted(names)                                                 
 >>> names
 ['Alex', 'Anne', 'Chris', 'Dora', 'Ethan',
 'John', 'Lizzie', 'Mike', 'Sarah', 'Wesley']
->>> names = sorted(names, key=len)                      
+>>> names = sorted(names, key=len)                                        
 >>> names
 ['Alex', 'Anne', 'Dora', 'John', 'Mike',
 'Chris', 'Ethan', 'Sarah', 'Lizzie', 'Wesley']
diff --git a/diveintopython3.org b/diveintopython3.org index ee9a8ef..f90dd34 100755 --- a/diveintopython3.org +++ b/diveintopython3.org @@ -13,6 +13,7 @@ * TODO 2nd draft Refactoring * TODO 1st draft Advanced Classes * TODO 1st draft Files + SCHEDULED: <2009-07-16 Thu> ** Reading from text files *** Opening a file (to read) *** Character encoding diff --git a/examples/plural4.py b/examples/plural4.py index dc7e2b8..1238623 100644 --- a/examples/plural4.py +++ b/examples/plural4.py @@ -15,7 +15,7 @@ def build_match_and_apply_functions(pattern, search, replace): return [matches_rule, apply_rule] rules = [] -with open('plural4-rules.txt') as pattern_file: +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( diff --git a/examples/plural5.py b/examples/plural5.py index 0a552c1..df475ad 100644 --- a/examples/plural5.py +++ b/examples/plural5.py @@ -15,7 +15,7 @@ def build_match_and_apply_functions(pattern, search, replace): return [matches_rule, apply_rule] def rules(rules_filename): - with open(rules_filename) as pattern_file: + with open(rules_filename, encoding='utf-8') as pattern_file: for line in pattern_file: pattern, search, replace = line.split(None, 3) yield build_match_and_apply_functions(pattern, search, replace) diff --git a/examples/plural6.py b/examples/plural6.py index 945803f..49ef865 100755 --- a/examples/plural6.py +++ b/examples/plural6.py @@ -18,7 +18,7 @@ class LazyRules: rules_filename = 'plural6-rules.txt' def __iter__(self): - self.pattern_file = open(self.rules_filename) + self.pattern_file = open(self.rules_filename, encoding='utf-8') self.cache = [] self.cache_index = 0 return self diff --git a/files.html b/files.html index a3bc05d..dbfc5ab 100644 --- a/files.html +++ b/files.html @@ -22,14 +22,6 @@ body{counter-reset:h1 12}

Diving In

FIXME - -

Reading From Text Files

FIXME @@ -41,7 +33,11 @@ open(..., 'r', encoding='...')

Character Encoding Rears Its Ugly Head

-

FIXME +

File Objects

@@ -134,6 +130,10 @@ ValueError: I/O operation on closed file

FIXME what's a "line"? (line endings discussion, universal line endings, etc.) + +

Writing to Text Files

FIXME @@ -195,7 +195,7 @@ test succeededline 2

  • At last, you handle your IOError exception. This could be the IOError exception raised by the call to open, seek, or read. Here, you really don’t care, because all you’re going to do is ignore it silently and continue. (Remember, pass is a Python statement that does nothing.) That’s perfectly legal; “handling” an exception can mean explicitly doing nothing. It still counts as handled, and processing will continue normally on the next line of code after the try...except block. --> -

    Binary Files

    +

    Binary Files

    FIXME diff --git a/generators.html b/generators.html index 32594aa..f85ad37 100755 --- a/generators.html +++ b/generators.html @@ -296,7 +296,7 @@ rules = []

    [download plural5.py]

    def rules():
    -    with open('plural5-rules.txt') as pattern_file:
    +    with open('plural5-rules.txt', encoding='utf-8') as pattern_file:
             for line in pattern_file:
                 pattern, search, replace = line.split(None, 3)
                 yield build_match_and_apply_functions(pattern, search, replace)
    @@ -376,7 +376,7 @@ def plural(noun):
     

    Let’s go back to plural5.py and see how this version of the plural() function works.

    def rules(rules_filename):
    -    with open(rules_filename) as pattern_file:
    +    with open(rules_filename, encoding='utf-8') as pattern_file:
             for line in pattern_file:
                 pattern, search, replace = line.split(None, 3)                   
                 yield build_match_and_apply_functions(pattern, search, replace)  
    diff --git a/iterators.html b/iterators.html
    index 7488376..c76c825 100755
    --- a/iterators.html
    +++ b/iterators.html
    @@ -218,7 +218,7 @@ All three of these class methods, __init__, __iter__,
         rules_filename = 'plural6-rules.txt'
     
         def __iter__(self):
    -        self.pattern_file = open(self.rules_filename)
    +        self.pattern_file = open(self.rules_filename, encoding='utf-8')
             self.cache = []
             self.cache_index = 0
             return self
    @@ -251,9 +251,9 @@ rules = LazyRules()
    class LazyRules:
         rules_filename = 'plural6-rules.txt'
     
    -    def __iter__(self):                                
    -        self.pattern_file = open(self.rules_filename)  
    -        self.cache = []                                
    +    def __iter__(self):                                                  
    +        self.pattern_file = open(self.rules_filename, encoding='utf-8')  
    +        self.cache = []                                                  
             self.cache_index = 0
    1. The __iter__() method is only going to be called once, after you instantiate the class, assign it to rules, and call iter(rules) to create the iterator.