parseing with python

Started by
5 comments, last by GameDev.net 19 years ago
ok i have try to look for find and parser in the help and can't seem to find what i am looking for. what i have it a doxufile that need to be built everynight and update the version number of the file automatically. now in the file the version looks like this: PROJECT_NUMBER = "#first#.#second#.#thrid# #version#" what i need to find out is how to find a certain text, replace it, and rewrite iot out to a different doxygen config file, can anyone help me?
Advertisement
Well, this is messy but I'm tired. It's not bad.
import redef run():    import re    # Our regex, basically look for something that is 1 or more numbers in between #'s    exp = re.compile( "#[0-9]+#")    # The beginning of the line we're looking for    lineBegin = "PROJECT_NUMBER = "    # New replacement values    firstRepl= 4    secondRepl = 19    thirdRepl = 25    versionRepl = 245    file = open("test.txt","r")    outFile = open("testout.txt","w")    replList = []    for line in file:        if line[0:len(lineBegin)] == lineBegin:            # Find all instances of matches of our regex, in iterator form            iter = exp.finditer(line)            for match in iter:                # Match.span() returns a tuple of (beginningIndex, endingIndex)                replList.append(match.span())            if len(replList) != 4:                print "Error: All items not found."                outFile.write(line)            else:                line = list(line)                # We want them backwards as inserting at beginning will mess                # up later indices                replList.reverse()                index = 4                for item in replList:                    if index == 4:                        # +1 and -1 to compensate for surrounding #'s                        line[item[0]+1:item[1]-1] = str(versionRepl)                    elif index == 3:                        line[item[0]+1:item[1]-1] = str(thirdRepl)                    elif index == 2:                        line[item[0]+1:item[1]-1] = str(secondRepl)                    elif index == 1:                        line[item[0]+1:item[1]-1] = str(firstRepl)                    index -= 1                outFile.write(''.join(line))        else:            outFile.write(line)    file.close()    outFile.close()if __name__ == "__main__":    run()

Which converted this:
TEST #4# TEST #4PROJECT_NUMBER = "#1#.#2#.#3# #456#"SOME OTHER INFO 1 2 3 ##33## #4#

to this:
TEST #4# TEST #4PROJECT_NUMBER = "#4#.#19#.#25# #245#"SOME OTHER INFO 1 2 3 ##33## #4#

Hopefully, that helps you out.
well now i want to replace #number_version# to like 0.0.1 and #name_version# to beta. it reads in from a templete file and then write out to a different file the is used my doxugen so the #number_varsion#, #name_version# is always there.how do i find #number_varsion#, #name_version#

First, let me clean up the code a bit:

import redef run(*replacements):    # Make a single regex for the line we are looking for.    # Look in the Python docs for regex syntax.    line_matcher = re.compile(      r"PROJECT_NUMBER = #[0-9]+#\.#[0-9]+#\.#[0-9]+# #[0-9]+#")    infile = open("test.txt","r")    outfile = open("testout.txt","w")    for line in infile:        if line_matcher.match(line):            # This line has the format indicated by the line, so output the            # replaced version.            outfile.write("PROJECT_NUMBER = %s.%s.%s %s" % replacements)        else:            outfile.write(line) # no replacement    infile.close()    outfile.close()if __name__ == "__main__":    run(4, 19, 25, 245)


In general, write an appropriate regex for the bit of text you want to find, and use the re module to do the search-and-replace. (In the sample code I match the whole line and output a whole replacement line manually instead).

Read the docs for the re module; if you're not familiar with regular expressions, you can [google] "regular expressions" and get pretty good results.

Although for simple literal strings like "#version_number#", this may be overkill. String objects in Python provide a 'replace' method that will return a version of self with the given replacement being done globally:

>>> "foo".replace("o", "bar")'fbarbar'>>> 


Thus you can build up a whole list of arguments for the replace - e.g. in a dict - and iterate through that:

for line in infile:  for key, value in replacements.items():    line = line.replace(key, value)  outfile.write(line)
ok the thing is i want to get rid of the ## that the number is inbetween and the first guys said is would look like:

#x number#.#x number#.#x number#

and well have version 1 looking like #1#.#0#.#0# looks really dumb. will what you said Zahlman get rid of the ##'s?

edit: what does this line of code do:?

if __name__ == "__main__":

[Edited by - 3dmodelerguy on March 20, 2005 5:39:00 PM]
ok,i can get it to write out but not to that perticular line i can get it either at the bottom or the top. how do i change a line with another line?
Quote:Original post by 3dmodelerguy
if __name__ == "__main__":
Execute the code after that line if that script is being launched itself (not imported etc).

This topic is closed to new replies.

Advertisement