Please help me get past this problem

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Nov 2009
Posts: 87
Reputation: P00dle is an unknown quantity at this point 
Solved Threads: 1
P00dle's Avatar
P00dle P00dle is offline Offline
Junior Poster in Training

Please help me get past this problem

 
0
  #1
Nov 4th, 2009
Hi.

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:
  1. import re
  2. special = re.compile(r"\\")
  3.  
  4. # This section writes the prefix to the text file
  5. foo = open("PREFIX.txt", "r+")
  6. text = foo.read()
  7. foo.close()
  8.  
  9. def escape(match):
  10. return "\\" + match.group(0)
  11.  
  12. #if __name__ == "__main__": <-- might need this in the future
  13. out = open("2Bxml.txt", "w")
  14. out.writelines(special.sub(escape, text))
  15. out.writelines("\n")
  16. out.close()
  17.  
  18. count1 = int(raw_input("Specify the number of repitations(e.g. \"1000\" will count from 1 to 999)"))
  19.  
  20. # This sections writes the suffix to the text file
  21. foo3 = open("SUFFIX.txt", "r+")
  22. text3 = foo3.read()
  23. foo3.close()
  24.  
  25. def escape(match):
  26. return "\\" + match.group(0)
  27.  
  28. out3 = open("2Bxml.txt", "a") # This must append, not just write
  29. out3.writelines(special.sub(escape, text3))
  30. out3.close()
Its works just fine. Can I use the existing regular expression code to do what I need to? Please help!

Thanks!
Last edited by P00dle; Nov 4th, 2009 at 6:42 am.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 1,311
Reputation: vishesh is on a distinguished road 
Solved Threads: 36
vishesh's Avatar
vishesh vishesh is offline Offline
Nearly a Posting Virtuoso
 
0
  #2
Nov 4th, 2009
Your regular expression would look like this:

  1. <%%>CNT(\d)+</%%>

We have put digits in a group.

And substitue it with <id>\\1</id> using sub method in re class. Here "\\1" indicated the group of above expression.

He
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Python Forum


Views: 217 | Replies: 1
Thread Tools Search this Thread



Tag cloud for Python
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC