Guest User

Untitled

a guest
Aug 19th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.88 KB | None | 0 0
  1. Python - Replace Value in Text at Random
  2. def replaceAll(file,searchExp,replaceExp):
  3. for line in fileinput.input(file, inplace=1):
  4. if searchExp in line:
  5. line = line.replace(searchExp,replaceExp)
  6. sys.stdout.write(line)
  7.  
  8. replaceAll('C:/Users/USERACCOUNT/test/test.js','InterSearchHere', RandomValueFrom2ndTXT)
  9.  
  10. import random
  11. import itertools as it
  12.  
  13. def replaceAll(file,searchExp,replaceExps):
  14. for line in fileinput.input(file, inplace=1):
  15. if searchExp in line:
  16. line = line.replace(searchExp,next(replaceExps))
  17. sys.stdout.write(line)
  18.  
  19. with open('SecondFile','r') as f:
  20. replaceExp=f.read().splitlines()
  21. random.shuffle(replaceExps) # randomize the order of the commands
  22. replaceExps=it.cycle(replaceExps) # so you can call `next(replaceExps)`
  23.  
  24. replaceAll('C:/Users/USERACCOUNT/test/test.js','InterSearchHere', replaceExps)
Add Comment
Please, Sign In to add comment