AttributeError: writer

Joseph M. Reagle Jr. reagle at MIT.EDU
Tue Jul 17 14:35:35 EDT 2001


I'm learning Python by working on a Canonical XML [1] implementation and the 
encoding issues, particularly with respect to example 6 [1] continue to 
confuse me.

[1] http://www.w3.org/TR/2001/REC-xml-c14n-20010315#Example-UTF8


First, I can't get the simple example from the Python Essential Reference 
(2.1) to work:

>import codecs
>(utf8_encode, utf8_decode, utf8_reader, utf8_writer) = \
>     codecs.lookup('utf-8')
>
>ustr = u'M\u00fcller'
>
>outf = utf8_writer(open('foo.xml','w'))
>outf.writer(ustr)
>outf.close

>Traceback (most recent call last):
>   File "./xpath.py", line 97, in ?
>     outf.writer(ustr)
>   File "d:\winapps\python21\lib\codecs.py", line 165, in __getattr__
>     return getattr(self.stream,name)
>AttributeError: writer

I'm not sure why I'm getting that error.


But I can get the following to work:
>(latin_encode, latin_decode, latin_reader, latin_writer) = \
>     codecs.lookup('iso-8859-1')
>infile = latin_reader(open('example-6.c14n'))
>eg6 = infile.read()
>infile.close()
>print eg6.encode('utf-8')

No problem with the reader, nor with foo.encode(''), just the writer.

Ultimately, I'd like to have the eg6 string (see attached) properly encoded 
as Latin, and output as UTF-8 as required by [1], but I get the typical '> 
128 error' when I try to write a serialization of that nodeset -- I suspect 
this is my own confusion, but I'm wondering if it stems from my first issue.



--
Regards,          http://www.mit.edu/~reagle/
Joseph Reagle     E0 D5 B2 05 B6 12 DA 65  BE 4D E3 C1 6A 66 25 4E
MIT LCS Research Engineer at the World Wide Web Consortium.

* This email is from an independent academic account and is
not necessarily representative of my affiliations.





More information about the Python-list mailing list