Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Python - Replace Value in Text at Random
- def replaceAll(file,searchExp,replaceExp):
- for line in fileinput.input(file, inplace=1):
- if searchExp in line:
- line = line.replace(searchExp,replaceExp)
- sys.stdout.write(line)
- replaceAll('C:/Users/USERACCOUNT/test/test.js','InterSearchHere', RandomValueFrom2ndTXT)
- import random
- import itertools as it
- def replaceAll(file,searchExp,replaceExps):
- for line in fileinput.input(file, inplace=1):
- if searchExp in line:
- line = line.replace(searchExp,next(replaceExps))
- sys.stdout.write(line)
- with open('SecondFile','r') as f:
- replaceExp=f.read().splitlines()
- random.shuffle(replaceExps) # randomize the order of the commands
- replaceExps=it.cycle(replaceExps) # so you can call `next(replaceExps)`
- replaceAll('C:/Users/USERACCOUNT/test/test.js','InterSearchHere', replaceExps)
Add Comment
Please, Sign In to add comment