Find the ID, but how to select/copy the whole string by ID?
f.guerrieri at gmail.com
Wed Sep 19 17:10:09 CEST 2007
On 9/19/07, Carsten Haese <carsten at uniqsys.com> wrote:
> , then looking for id 12 is going to match on the wrong ID. Besides,
> that code only tells you where something that looks like the ID you're
> looking for is in the file. There is no guarantee that the match
> actually occurs inside an ID attribute. It also doesn't help in
> retrieving the text contents of the <str> tag that has this ID.
> If your input is an XML file, using an actual XML parser is the only
> correct solution.
You're perfectly right.
The code example was purposedly incomplete for this very reason.
It could be made less sensitive to false matches by constructing a
correct substring, something like
pattern = '<str id = ' + stringID + '>'
and then in the loop:
but I agree that it would be wrong to proceed this way.
The motivation of my reply was more to suggest a better way to iterate
over a file than else... but since I've been confusing it probably
would have been better to avoid.
More information about the Python-list