Guest User

Untitled

a guest
Oct 16th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.21 KB | None | 0 0
  1. input.txt
  2.  
  3. ...text... <!-- 140101002 --> ...text...
  4. ...text... <!-- 140101030 --> ...text...
  5. ...text... <!-- 140101004 --> ...text...
  6. ...text... <!-- 140101058 --> ...text...
  7.  
  8. ...text... <!-- name44 --> ...text...
  9. ...text... <!-- name2 --> ...text...
  10. ...text... <!-- name3 --> ...text...
  11. ...text... <!-- name94 --> ...text...
  12.  
  13. fhand = open("/home/Documents/input.txt")
  14. template = open("/home/Documents/template.txt")
  15. fout = open("/home/Documents/output.txt","w")
  16.  
  17. # store the values in the dictionary:
  18. templateDict = dict()
  19. for line in template:
  20. lineList = line.split("t")
  21. templateDict[lineList[0]] = lineList[1]
  22.  
  23. # replacement:
  24. for line in fhand:
  25. for key in templateDict:
  26. if key in line2:
  27. line2 = line2.replace(key,templateDict[key])
  28. fout.write(line2) # write to output.txt
  29.  
  30. fout.close()
  31.  
  32. import re
  33. d = dict(re.split('s+', i.strip('n')) for i in open('filename.txt'))
  34. content = [i.strip('n') for i in open('filename2.txt')]
  35. with open('results.txt', 'w') as f:
  36. f.write('n'.join(re.sub('(?<=<!--s)d+(?=s-->)', lambda x:d[x.group()], i) for i in content))
  37.  
  38. ...text... <!-- name44 --> ...text...
  39. ...text... <!-- name2 --> ...text...
  40. ...text... <!-- name3 --> ...text...
  41. ...text... <!-- name94 --> ...text...
Add Comment
Please, Sign In to add comment