Jump to content
  • Advertisement
Sign in to follow this  
3dmodelerguy

parseing with python

This topic is 4888 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
Advertisement
Well, this is messy but I'm tired. It's not bad.

import re

def 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 #4
PROJECT_NUMBER = "#1#.#2#.#3# #456#"
SOME OTHER INFO 1 2 3 ##33## #4#



to this:

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



Hopefully, that helps you out.

Share this post


Link to post
Share on other sites
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#

Share this post


Link to post
Share on other sites
First, let me clean up the code a bit:


import re

def 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)

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Quote:
Original post by 3dmodelerguy
if __name__ == "__main__":
Execute the code after that line if that script is being launched itself (not imported etc).

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!