Search code examples
python-2.7encryptionencodingdecodingread-write

Python Read then Write project


I am trying to write a program that will read a text file and convert what it reads to another text file but using the given variables. Kinda like a homemade encryption. I want the program to read 2 bytes at a time and read the entire file. I am new to python but enjoy the application. any help would be greatly appreciated

a = 12
b = 34
c = 56
etc... up to 20 different types of variables

file2= open("textfile2.text","w")
file = open("testfile.txt","r")
file.read(2):
if file.read(2) = 12 then;
file2.write("a")
else if file.read(2) = 34
file2.write("b")
else if file.read(2) = 56
file2.write("c")
file.close()
file2.close()


Text file would look like:
1234567890182555

so the program would read 12 and write "a" in the other text file and then read 34 and put "b" in the other text file. Just having some logic issues.


Solution

  • I like your idea here is how I would do it. Note I convert everything to lowercase using lower() however if you understand what I am doing it would be quite simple to extend this to work on both lower and uppercase:

    import string
    
    d = dict.fromkeys(string.ascii_lowercase, 0) # Create a dictionary of all the letters in the alphabet
    
    updates = 0
    while updates < 20: # Can only encode 20 characters
        letter = input("Enter a letter you want to encode or type encode to start encoding the file: ")
        if letter.lower() == "encode": # Check if the user inputed encode
            break
        if len(letter) == 1 and letter.isalpha(): # Check the users input was only 1 character long and in the alphabet
            encode = input("What do want to encode %s to: " % letter.lower()) # Ask the user what they want to encode that letter to
            d[letter.lower()] = encode
            updates += 1
        else:
            print("Please enter a letter...")
    
    with open("data.txt") as f:
        content = list(f.read().lower())
    
    for idx, val in enumerate(content):
        if val.isalpha():
            content[idx] = d[val]
    
    with open("data.txt", 'w') as f:
        f.write(''.join(map(str, content)))
    
    print("The file has been encoded!")
    

    Example Usage:

    Original data.txt:

    The quick brown fox jumps over the lazy dog
    

    Running the script:

    Enter a letter you want to encode or type encode to start encoding the file: T                                                                                                                                                                                          
    What do want to encode t to: 6                                                                                                                                                                                                                                          
    Enter a letter you want to encode or type encode to start encoding the file: H                                                                                                                                                                                          
    What do want to encode h to: 8                                                                                                                                                                                                                                          
    Enter a letter you want to encode or type encode to start encoding the file: u                                                                                                                                                                                          
    What do want to encode u to: 92                                                                                                                                                                                                                                         
    Enter a letter you want to encode or type encode to start encoding the file: 34                                                                                                                                                                                         
    Please enter a letter...                                                                                                                                                                                                                                                
    Enter a letter you want to encode or type encode to start encoding the file: rt                                                                                                                                                                                         
    Please enter a letter...                                                                                                                                                                                                                                                
    Enter a letter you want to encode or type encode to start encoding the file: q                                                                                                                                                                                          
    What do want to encode q to: 9                                                                                                                                                                                                                                          
    Enter a letter you want to encode or type encode to start encoding the file: encode                                                                                                                                                                                     
    The file has been encoded!
    

    Encode data.txt:

    680 992000 00000 000 092000 0000 680 0000 000