ElementTree should parse string and file in the same way
Steven D'Aprano
steve at REMOVE-THIS-cybersource.com.au
Tue Jan 1 17:01:55 EST 2008
On Tue, 01 Jan 2008 12:59:44 -0700, Steven Bethard wrote:
> Steven D'Aprano wrote:
>> On Tue, 01 Jan 2008 13:36:57 +0100, Diez B. Roggisch wrote:
>>
>>> And codemonkeys know that in python
>>>
>>> doc = et.parse(StringIO(string))
>>>
>>> is just one import away
>>
>> Yes, but to play devil's advocate for a moment,
>>
>> doc = et.parse(string_or_file)
>>
>> would be even simpler.
>
> I assume the problem with this is that it would be ambiguous. You can
> already use either a string or a file with ``et.parse``. A string is
> interpreted as a file name, while a file object is used directly.
Ah! I wasn't aware that parse() operated on either an open file object or
a string file name. That's an excellent reason for not treating strings
the same as files in ElementTree.
> How would you differentiate between a string that's supposed to be a
> file name, and a string that's supposed to be XML?
Well, naturally I wouldn't.
I *could*, if I assumed that a multi-line string that started with "<"
was XML, and a single-line string with the path separator character or
ending in ".xml" was a file name, but that sort of Do What I Mean coding
is foolish in a library function that can't afford to occasionally Do The
Wrong Thing.
--
Steven
More information about the Python-list
mailing list