944,014 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Marked Solved
  • Views: 393
  • Python RSS
Nov 4th, 2009
0

Please help me get past this problem

Expand Post »
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:
Python Syntax (Toggle Plain Text)
  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.
Similar Threads
Reputation Points: 10
Solved Threads: 1
Junior Poster
P00dle is offline Offline
154 posts
since Nov 2009
Nov 4th, 2009
0
Re: Please help me get past this problem
Your regular expression would look like this:

Python Syntax (Toggle Plain Text)
  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
Reputation Points: 85
Solved Threads: 42
Nearly a Posting Virtuoso
vishesh is offline Offline
1,362 posts
since Oct 2006

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: palindrome
Next Thread in Python Forum Timeline: Need help please!





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC