First, I'll explain my task:
I have 4 text files. 3 of the files contain xml tags and content. The 4th is empty.
I need to read the 3 files, and write them to the 4th file. The reason for having 3 files is that the first and the last files both only have to be used once. The 2nd file has to be repeated a user-specified amount of times.
Initially my problem was that I had \'s in the text, and didnt know how to use regular expressions to escape them. That as been solved via DaniWeb :)
I have since advanced the code a bit, but am now stuck again. The body(2nd text file) needs to be repeated a certain amount of times. This can be done with a counter. Easy. The problem is that the xml in the text file contains an "id" tag that needs to hold the count value for every repition, in order to be unique. I can't figure out how to use regular expressions to find that tag and give it the value. Let's pretend the tag looks like this: <%%>CNT1</%%>
I need to replace <%%>CNt1</%%> with
<id>"value of count"</id>
My code, so far, looks like this:
import re special = re.compile(r"\\") # This section writes the prefix to the text file foo = open("PREFIX.txt", "r+") text = foo.read() foo.close() def escape(match): return "\\" + match.group(0) #if __name__ == "__main__": <-- might need this in the future out = open("2Bxml.txt", "w") out.writelines(special.sub(escape, text)) out.writelines("\n") out.close() count1 = int(raw_input("Specify the number of repitations(e.g. \"1000\" will count from 1 to 999)")) # This sections writes the suffix to the text file foo3 = open("SUFFIX.txt", "r+") text3 = foo3.read() foo3.close() def escape(match): return "\\" + match.group(0) out3 = open("2Bxml.txt", "a") # This must append, not just write out3.writelines(special.sub(escape, text3)) out3.close()
Its works just fine. Can I use the existing regular expression code to do what I need to? Please help!