jeudi 28 décembre 2017

Find, replace and print out updated string

I am trying to create a python script that has 10-20 lines of fixed data in a string with 2 special character's that need to be replaced with a different, random string using randomword()

import random, string

def randomword(length):
   letters = string.ascii_lowercase
   return ''.join(random.choice(letters) for i in range(length))


junk = """
random_string1 = {};
random_string2 = {};
random_string3 = {};
random_string4 = {};
random_string5 = {};
"""

stra = string.replace(junk, '{}', randomword(40))
print (stra)

The two special characters in the string are {}, I would like to iterate through the string to find those characters and replace them with a different random string generated by randomword()

Above is as far as I got, this piece of code replaces all of the occurrences of {} with a random string, but they have the same values, I would like to have differing values for each {}.

I don't know how to put this into a loop. Any help is appreciated.




Aucun commentaire:

Enregistrer un commentaire