I'm having trouble using elementtree with an XML file that has some
gbk-encoded text. (I can't read Chinese, so I'm taking their word for
it that it's gbk-encoded.) I always have trouble with encodings, so I'm
sure I'm just screwing something simple up. Can anyone help me?
Here's the interactive session. Sorry it's a little verbose, but I
figured it would be better to include too much than not enough. I
basically expected et.ElementTree( file=...) to fail since no encoding
was specified, but I don't know what I'm doing wrong when I use
codecs.open(... )
Thanks in advance for the help!
[color=blue][color=green][color=darkred]
>>> import elementtree.Ele mentTree as et
>>> import codecs
>>> et.ElementTree( file=filename)[/color][/color][/color]
Traceback (most recent call last):
File "<interacti ve input>", line 1, in ?
File "C:\Program
Files\Python\li b\site-packages\elemen ttree\ElementTr ee.py", line 543, in
__init__
self.parse(file )
File "C:\Program
Files\Python\li b\site-packages\elemen ttree\ElementTr ee.py", line 583, in
parse
parser.feed(dat a)
File "C:\Program
Files\Python\li b\site-packages\elemen ttree\ElementTr ee.py", line 1242,
in feed
self._parser.Pa rse(data, 0)
ExpatError: not well-formed (invalid token): line 8, column 6[color=blue][color=green][color=darkred]
>>> et.ElementTree( file=codecs.ope n(filename, 'r', 'gbk'))[/color][/color][/color]
Traceback (most recent call last):
File "<interacti ve input>", line 1, in ?
File "C:\Program
Files\Python\li b\site-packages\elemen ttree\ElementTr ee.py", line 543, in
__init__
self.parse(file )
File "C:\Program
Files\Python\li b\site-packages\elemen ttree\ElementTr ee.py", line 583, in
parse
parser.feed(dat a)
File "C:\Program
Files\Python\li b\site-packages\elemen ttree\ElementTr ee.py", line 1242,
in feed
self._parser.Pa rse(data, 0)
UnicodeEncodeEr ror: 'ascii' codec can't encode characters in position
133-135: ordinal not in range(128)[color=blue][color=green][color=darkred]
>>> text = open(filename). read()
>>> text[/color][/color][/color]
'<DOC>\n<DOCID> ART242</DOCID>\n<HEADER >\n
<DATE></DATE>\n</HEADER>\n<BODY> \n<HEADLINE>\n< S ID=2566>\n( (IP-HLN
(LCP-TMP (IP (NP-PN-SBJ (NR \xb7\xfc\xc3\xf 7\xcf\xbc)) \n\t\t (VP
(VV \xbb\xf1\xb5\xc 3) \n\t\t\t (NP-OBJ (NN \xc5\xae\xd7\xd 3)
\n\t\t\t\t (NN \xcc\xf8\xcc\xa 8) \n\t\t\t\t (NN \xcc\xf8\xcb\xa e)
\n\t\t\t\t (NN \xb9\xda\xbe\xf c)))) \n\t\t (LC \xba\xf3)) \n
(PU \xa3\xac) \n (NP-SBJ (NP-PN (NR
\xcb\xd5\xc1\xa a\xb6\xd3)) \n (NP (NN
\xbd\xcc\xc1\xb 7))) \n (VP (ADVP (AD \xc8\xc8\xc7\xe 9)) \n
(PP-DIR (P \xcf\xf2) \n\t\t (NP (PN \xcb\xfd))) \n
(VP (VV \xd7\xa3\xba\xd 8))) \n (PU \xa1\xa3)) )
\n</S>\n<S ID=2567>\n( (FRAG (NR \xd0\xc2\xbb\xa a\xc9\xe7) \n
(NN \xbc\xc7\xd5\xd f) \n (NR \xb3\xcc\xd6\xc 1\xc9\xc6) \n
(VV \xc9\xe3) )) \n</S>\n</HEADLINE>\n<TEX T>\n</TEXT>\n</BODY>\n</DOC>\n'
STeVe
gbk-encoded text. (I can't read Chinese, so I'm taking their word for
it that it's gbk-encoded.) I always have trouble with encodings, so I'm
sure I'm just screwing something simple up. Can anyone help me?
Here's the interactive session. Sorry it's a little verbose, but I
figured it would be better to include too much than not enough. I
basically expected et.ElementTree( file=...) to fail since no encoding
was specified, but I don't know what I'm doing wrong when I use
codecs.open(... )
Thanks in advance for the help!
[color=blue][color=green][color=darkred]
>>> import elementtree.Ele mentTree as et
>>> import codecs
>>> et.ElementTree( file=filename)[/color][/color][/color]
Traceback (most recent call last):
File "<interacti ve input>", line 1, in ?
File "C:\Program
Files\Python\li b\site-packages\elemen ttree\ElementTr ee.py", line 543, in
__init__
self.parse(file )
File "C:\Program
Files\Python\li b\site-packages\elemen ttree\ElementTr ee.py", line 583, in
parse
parser.feed(dat a)
File "C:\Program
Files\Python\li b\site-packages\elemen ttree\ElementTr ee.py", line 1242,
in feed
self._parser.Pa rse(data, 0)
ExpatError: not well-formed (invalid token): line 8, column 6[color=blue][color=green][color=darkred]
>>> et.ElementTree( file=codecs.ope n(filename, 'r', 'gbk'))[/color][/color][/color]
Traceback (most recent call last):
File "<interacti ve input>", line 1, in ?
File "C:\Program
Files\Python\li b\site-packages\elemen ttree\ElementTr ee.py", line 543, in
__init__
self.parse(file )
File "C:\Program
Files\Python\li b\site-packages\elemen ttree\ElementTr ee.py", line 583, in
parse
parser.feed(dat a)
File "C:\Program
Files\Python\li b\site-packages\elemen ttree\ElementTr ee.py", line 1242,
in feed
self._parser.Pa rse(data, 0)
UnicodeEncodeEr ror: 'ascii' codec can't encode characters in position
133-135: ordinal not in range(128)[color=blue][color=green][color=darkred]
>>> text = open(filename). read()
>>> text[/color][/color][/color]
'<DOC>\n<DOCID> ART242</DOCID>\n<HEADER >\n
<DATE></DATE>\n</HEADER>\n<BODY> \n<HEADLINE>\n< S ID=2566>\n( (IP-HLN
(LCP-TMP (IP (NP-PN-SBJ (NR \xb7\xfc\xc3\xf 7\xcf\xbc)) \n\t\t (VP
(VV \xbb\xf1\xb5\xc 3) \n\t\t\t (NP-OBJ (NN \xc5\xae\xd7\xd 3)
\n\t\t\t\t (NN \xcc\xf8\xcc\xa 8) \n\t\t\t\t (NN \xcc\xf8\xcb\xa e)
\n\t\t\t\t (NN \xb9\xda\xbe\xf c)))) \n\t\t (LC \xba\xf3)) \n
(PU \xa3\xac) \n (NP-SBJ (NP-PN (NR
\xcb\xd5\xc1\xa a\xb6\xd3)) \n (NP (NN
\xbd\xcc\xc1\xb 7))) \n (VP (ADVP (AD \xc8\xc8\xc7\xe 9)) \n
(PP-DIR (P \xcf\xf2) \n\t\t (NP (PN \xcb\xfd))) \n
(VP (VV \xd7\xa3\xba\xd 8))) \n (PU \xa1\xa3)) )
\n</S>\n<S ID=2567>\n( (FRAG (NR \xd0\xc2\xbb\xa a\xc9\xe7) \n
(NN \xbc\xc7\xd5\xd f) \n (NR \xb3\xcc\xd6\xc 1\xc9\xc6) \n
(VV \xc9\xe3) )) \n</S>\n</HEADLINE>\n<TEX T>\n</TEXT>\n</BODY>\n</DOC>\n'
STeVe
Comment