import re file = open(r'blog.xml') s = file.readlines() file.close() t = 0 d = 0 l = 0 time = {} content = {} for line in s: date = re.findall('.*',line) if len(word)==1: time[t] = [word[0],l] t+=1 if len(date)==1: content[d] = l d+=1 l+=1 if t == d: for n in content: s.insert(content[n],time[n][0]) s.pop(time[n][1]+1) file = open('test.xml','w') file.writelines(s) file.close()