dimanche 30 octobre 2016

How to compare random generated string with a string on txt file - Python

I'm generating a random string of 28 characters, I need to compare this generated string with a another string which is on a file, this is my code at the moment:

import string
import random

file = open('file.dat', 'r')
file=file.read()
print file

def id_generator(size=28, chars=string.ascii_uppercase + string.digits):
    return ''.join(random.choice(chars) for _ in range(size))
file = open("newfile.txt", "w")
file.write(id_generator())
file.close()

This code, opens file.dat prints it on console, then generates a random string, stores the string on file and exits, my problem is that I want to compare the file.dat content which is a string, and the random generated one. And I can't seem to clear my way onto a nice way to accomplish this.

I've read this answer but I'm cofused about it, because in this case, there is a random generated string, and another one which is on a file.

What could be the best approach to accomplish this?

I hope I've explained myself.

EDIT

The underlying idea, is to compare strings, let's say on file.dat I have "THIS IS A STRING", then compare the random generated one and see if any random generated character matches the position of characters in the phrase present on file.dat




Aucun commentaire:

Enregistrer un commentaire