mercredi 21 janvier 2015

python randomizer - get random text between curly braces with double nesting level

hey i need to create simple python randomizer. example input:



{{hey|hello|hi}|{privet|zdravstvuy|kak dela}|{bonjour|salut}}, can {you|u} give me advice?


and output should be:



hello, can you give me advice


i have a script, which can do this but only in one nested level



with open('text.txt', 'r') as text:
matches = re.findall('([^{}]+)', text.read())
words = []
for match in matches:
parts = match.split('|')
if parts[0]:
words.append(parts[random.randint(0, len(parts)-1)])
message = ''.join(words)


this is not enough for me )





Aucun commentaire:

Enregistrer un commentaire