I am trying to make a script which finds everything between a symbol {} in a text document. It takes the .txt documents specific part in the {} and organizes it alphabetically, then writing it inplace back to the text document. Example of text document..
bla bla bla
bla ba bl bla ba bl {apple:banana, this: something else, airplane:hobby}
bla bla bla
bla bla bla
Desired output(sorted alphabetically)..
bla bla bla
bla ba bl bla ba bl {airplane:hobby, apple:banana, this: something else}
bla bla bla
bla bla bla
What its still printing..
bla bla bla
bla ba bl bla ba bl {apple:banana, this: something else, airplane:hobby}
bla bla bla
bla bla bla
My code..
def openFind():
f = open(inFile, 'r')
lines = f.read()
match = re.findall(r'{(.*?)}', lines)
before = str(match)
n=1
for i in xrange(0, len(match), n):
mydict = match[i:i+n]
for x in sorted(mydict):
c = x.split(',')
newmatch = sorted(c)
final = str(newmatch)
print final
# NOT WORKING BELOW!!! Stuck in loop?
with open(outFile,'w') as new_file:
with open(inFile) as old_file:
for line in old_file:
new_file.write(line.replace(before, after))
It prints the sorted/alphabetical list as [airplane:hobby, apple:banana, this: something else], but how do I get it to replace the original text in the text document? Has to be inplace, but can make a new txt.