| | |
Please help me get past this problem
Thread Solved |
•
•
Join Date: Nov 2009
Posts: 65
Reputation:
Solved Threads: 0
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; 27 Days Ago at 6:42 am.
0
#2 26 Days Ago
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!
| Thread Tools | Search this Thread |
address anydbm app bash beginner changecolor cipher clear conversion coordinates corners curves definedlines development dictionary dynamic events examples excel feet file float format function generator getvalue gui handling homework images import input ip java keycontrol line linux list lists loan loop maintain matching maze millimeter mouse mysqldb number numbers output parsing path port prime programming projects py2exe pygame pymailer python queue random rational raw_input recursion recursive scrolledtext searchingfile shebang singleton slicenotation split string strings table tails terminal text thread threading time tlapse tooltip tuple tutorial type ubuntu unicode url urllib urllib2 valueerror variable variables vigenere web word wx.wizard wxpython xlwt






