![]() |
|
|
|||||||
![]() |
Python - Re: ElementTree: How to return only unicode? |
|
|
Thread Tools | Search this Thread |
|
|
#1 |
|
Torsten Bronger wrote:
> I parse an XML file with ElementTree and get the contets with > the .attrib, .text, .get etc methods of the tree's nodes. > Additionally, I use the "find" and "findtext" methods. > > My problem is that if there is only ASCII, these methods return > ordinary strings instead of unicode. So sometimes I get str, > sometimes I get unicode. Can one change this globally so that they > only return unicode? That's a convenience measure to reduce memory and processing overhead. Could you explain why this is a problem for you? Stefan Stefan Behnel |
|
|
|
|
#2 |
|
Posts: n/a
|
Torsten Bronger wrote:
> Hallöchen! und zurück! > Stefan Behnel writes: > >> Torsten Bronger wrote: >> >>> [...] >>> >>> My problem is that if there is only ASCII, these methods return >>> ordinary strings instead of unicode. So sometimes I get str, >>> sometimes I get unicode. Can one change this globally so that >>> they only return unicode? >> That's a convenience measure to reduce memory and processing >> overhead. > > But is this really worth the inconsistency of having partly str and > partly unicode, given that the common origin is unicode XML data? Yes. It's no difference in almost all use cases, as long as you assume Py2 string handling semantics. In Py3, you will always get Unicode strings anyway. >> Could you explain why this is a problem for you? > > I feed ElementTree's output to functions in the unicodedata module. > And they want unicode input. While it's not a big deal to write > e.g. unicodedata.category(unicode(my_character)), I find this rather > wasteful. I just looked at the code. It seems that you can use your own XMLTreeBuilder subclass and overwrite the "._fixtext()" method like this: def _fixtext(self, text): return text Then pass an instance of that as "parser" when parsing in ElementTree. That should do the trick. Stefan Stefan Behnel |
|
![]() |
| Thread Tools | Search this Thread |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| DVD Verdict reviews: COWBOY BEBOP REMIX 2, JANE GOODALL'S RETURN TO GOMBE, and more! | DVD Verdict | DVD Video | 0 | 12-22-2005 09:14 AM |
| DVD Verdict reviews: DAIMAJIN / RETURN OF DAIMAJIN / WRATH OF DAIMAJIN and more! | DVD Verdict | DVD Video | 0 | 05-24-2005 09:13 AM |
| DVD Verdict reviews: THE LORD OF THE RINGS: THE RETURN OF THE KING: SPECIAL EXTENDED EDITION and more! | DVD Verdict | DVD Video | 0 | 01-25-2005 10:34 AM |
| Return Of The King - UK Region 2 - Full DVD Details | Chris - DVD Debate | DVD Video | 0 | 02-08-2004 06:39 PM |
| DVD Verdict reviews: BARBIE OF SWAN LAKE, THE RETURN OF SWAMP THING, and more! | DVD Verdict | DVD Video | 0 | 11-22-2003 10:04 AM |