On Wed, 2007-09-19 at 16:41 +0200, Francesco Guerrieri wrote: > On 9/19/07, Leon <[EMAIL PROTECTED]> wrote: > > stringID = str(raw_input('Enter the string ID : ')) > > file = open('strings.txt') > > sourcefile = file.read() > > file.close() > > sourcefile.find (stringID) > > > > but how can I select and copy the specific string from <str> to </str> > > with id I input? > > If the file you are parsing is in xml, there are many parser out there > which can help you (this is discussed very often on this list, even > today) . > > If the format is very simple and you really want to do it by hand, you > could do something like: > > stringID = raw_input('Enter the string ID:') > for line in open('strings.txt'): > if line.find(stringID) > -1: > print 'Found!'
Using find() for XML parsing is like using a machine gun to pound a nail into a wall. It is absolutely the wrong tool for the job. If the input file looks something like this: <str id="123">blah</str> <str id="12">spam</str> , 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. -- Carsten Haese http://informixdb.sourceforge.net -- http://mail.python.org/mailman/listinfo/python-list