1

I need to read from file, two strings, one 'static' string from file, and one random dynamically generated one, which is also written on a file.

Then, replace one character in the random generated string with another random one.

And repeat the process. Until I get the "static" string which is on a file, in this case "THIS IS A STRING".

I'm pretty lost trying to achieve this, this is what I have so far:

import string
import random
import os
import re

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

def id_generator(size=28, chars=string.ascii_uppercase + string.digits):
    return ''.join(random.choice(chars) for _ in range(size))
if os.path.exists('newfile.txt'):
   os.remove('newfile.txt')
else:
    file = open("newfile.txt", "w")
    file.write(id_generator())
    file.close()
    if re.search(r"THIS IS A STRING", file):
        print("success!")

I'm trying to achieve this with re module, since it should read character by character, finding it's position in the random generated string.

Not just comparing but also finding the position of the matching characters (if any)

The file.dat file contains the THIS IS A STRING string, which I call the 'static' one, it doesn't changes, it should be matched by the random generated ones process .

The newfile.txt prints the random generated string.

So, in a nutshell, how can I read the string on file.dat character by character, and same goes for the random generated string on newfile.txt?

I hope I've explained myself.

4
  • 3
    Not sure why you need regular expressions for this, though I don't fully understand the goal. But str.replace, str.index and string index str[i] seem to be enough for purposes. Commented Nov 1, 2016 at 4:21
  • 1
    Please fix your code block; part of the code falls outside the code block formatting, making things harder to read. Commented Nov 1, 2016 at 4:23
  • Done, Sorry, Thank You, Without regex you think? Even character by character? Commented Nov 1, 2016 at 4:24
  • 2
    You don't need to check if the file exist, and then remove it. w will create a file, and if one exists, delete all its contents. You are also running re.search against a closed file handle - and that won't give you your expected results. Commented Nov 1, 2016 at 5:18

1 Answer 1

1

At the least, you should probably change this

file=file.read().split(',')
print file

and this

    if re.search(r"THIS IS A STRING", file):
        print("success!")

to this

data=file.read().split(',')
print data

and this

    if re.search(r"THIS IS A STRING", data):
        print("success!")

You are losing the file contents when you open the output file:

else:
    file = open("newfile.txt", "w")
Sign up to request clarification or add additional context in comments.

6 Comments

Hi, Thank You, but I dont understand, your changes are for the 'file.dat' file, not for the random generated one?
I modified the answer to show you the code that overwrites the file variable.
So, I have to do something else? Comment that else statement right?
Just applied your answer is just the same thing, and what if I comment else statement? Do I have to do that? modify that?
You have to use a different variable to store the file contents. In this case, I suggested the name data.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.