jeudi 4 janvier 2018

Using a variable inside a Dictionary in Python 2.7

So I'm trying to replace all blank space in a given txt for random numbers. I've tried to find the way to do it but did not managed to. It looks simple:

from random import randint
space = (randint(0, 9))
print space
replacements = {' ': %space, 'a':'w'}

with open('texto.txt') as infile, open('textocod.txt', 'w') as outfile:
    for line in infile:
        for src, target in replacements.iteritems():
            line = line.replace(src, target)
        outfile.write(line)

How could I replace every blank space with a random value from 'space' variable?

Thank you in advance.




Aucun commentaire:

Enregistrer un commentaire