Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import re
- #variables to set
- origFileName = "orig.txt"
- newFileName = "new.txt"
- relfirst = "relfirst"
- relsecond = "relsecond"
- #Reads the old file in, probably slow if your file is very big, but you didn't specify that
- textfile = open(origFileName, 'r')
- filetext = textfile.read()
- textfile.close()
- #This is called a regular expression, its a fancy way to find something, look it up online
- pattern = re.compile("(?<=<"+relfirst+">).*?(?=</"+relfirst+">)|(?<=<"+relsecond+">).*?(?=</"+relsecond+">)",re.DOTALL)
- matches = pattern.findall(filetext)
- if(len(matches)>0):
- #open the new file where we write the result
- newfile = open(newFileName,'w')
- #write the start of the thing, you might want to tweak this
- newfile.write("""<?xml version="1.0"?>
- <fixed>
- <useless>0</useless>""")
- i=0
- #Run through all the items in the file
- while i < len(matches):
- firstStringYouWanted = matches[i];
- secondStringYouWanted = matches[i+1];
- #Write each item into new file, you might want to tweak the stuff below
- newfile.write("""
- <fixeditem>
- <type>s</type>
- <first>"""+firstStringYouWanted+"""</first>
- <second>"""+secondStringYouWanted+"""</second>
- </fixeditem>""")
- i=i+2
- #write the last part, you might want to tweak this
- newfile.write("\n</fixed>")
- newfile.close()
- print("Sucessfully searched/replaced everything")
- else:
- print("No match found")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement