| | |
Please help me get past this problem
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
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:
Its works just fine. Can I use the existing regular expression code to do what I need to? Please help!
Thanks!
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)
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()
Thanks!
Last edited by P00dle; Nov 4th, 2009 at 6:42 am.
0
#2 Nov 4th, 2009
Your regular expression would look like this:
We have put digits in a group.
And substitue it with
He
Python Syntax (Toggle Plain Text)
<%%>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
![]() |
Similar Threads
- HELP! Toshiba Tecra 8100 harddrive / boot problem (Troubleshooting Dead Machines)
- C# Winsock (C#)
- Generic Host Process/svchost.exe Problem (Windows NT / 2000 / XP)
- As crazy as this sounds... (DaniWeb Community Feedback)
- Please Help, It's been a while (ASP.NET)
- Please help, been a few years. (JavaScript / DHTML / AJAX)
- i finally did it (*nix Software)
- Password forgotten (Windows NT / 2000 / XP)
- my IE has been hijacked by res://mbnuq.dll/index.html#96676 (Viruses, Spyware and other Nasties)
Other Threads in the Python Forum
- Previous Thread: palindrome
- Next Thread: Need help please!
Views: 217 | Replies: 1
| Thread Tools | Search this Thread |
Tag cloud for Python
approximation array beginner book builtin change cipher clear client code color converter countpasswordentry cturtle curved def dictionary drive dynamic examples excel file float format ftp function gui homework import inches input java library line lines linux list lists loop microcontroller mouse mysqldb mysqlquery newb number numbers output parsing path plugin port prime program programming projects py2exe pygame pymailer pyqt python random recursion recursive redirect remote script scrolledtext search singleton socket sqlite ssh string strings strip subprocess sum syntax table terminal text textarea thread threading time tkinter tlapse tuple tutorial twoup ubuntu unicode urllib urllib2 variable vigenere wikipedia windows wxpython xlwt






