Note that there are some explanatory texts on larger screens.

plurals
  1. POUse substitution cipher in python to encrypt and decrypt a .txt file and output to a new .txt file
    primarykey
    data
    text
    <p>I am able to open the rules file and create a dictionary to use for my encryption. I have to also create a dictionary to use for decrypting text. I assume it's basically the same function with minor changes. The encrypt works fine, but I can't get the decrypt to work. My second problem is that while I encrypted the file I took out all spaces and punctuation. I can't figure out how to get those back in the output file once I run the program. It just prints in a single column. Lastly I have to output this to a .txt file. I am able to create a .txt with a user assigned name, but can't get anything to print on the file. </p> <p>Here is what I achieved so far. </p> <pre><code>#import clauses import string #function definitions #encrypt dictionary def createrulesdictencrypt(openFile): rulesencrypt1 = {} for line in openFile: rulessplit = string.split(string.strip(line)) rulesencrypt1[rulessplit[0]] = rulessplit[1] return rulesencrypt1 #decrypt dictionary def createrulesdictdecrypt(openFile): rulesdecrypt1 = {} for line in openFile: rulessplit = string.split(string.strip(line)) rulesdecrypt1[rulessplit[1]] = rulessplit[0] return rulesdecrypt1 openFile = open('rules.txt', 'r') rulesencrypt = createrulesdictencrypt(openFile) rulesdecrypt = createrulesdictdecrypt(openFile) #print rulesencrypt #print rulesdecrypt #function for encrypting file def encryptfile(openFile2): for line in openFile2: for word in line.split(): empty = '' for char in word: if char not in string.punctuation: char=char.lower() empty = empty+char if len(empty) == 2: print rulesencrypt[empty] empty = '' if len(empty) == 1: print rulesencrypt[empty] #function for decrypting file def decryptfile(openFile2): for line in openFile2: for word in line.split(): empty = '' for char in word: if char not in string.punctuation: char=char.lower() empty = empty+char if len(empty) == 2: print rulesdecrypt[empty] empty = '' if len(empty) == 1: print rulesdecrypt[empty] #main program ende = raw_input("To encrypt a file, enter '0':\nTo decrypt a file, enter '1':") filename = raw_input("Enter the name of the file to be processed:") outfilename = raw_input("Enter the name of the file to save the result to:") openFile2 = open(filename, 'r') outputfile = open(outfilename, 'w') fileencrypt = encryptfile(openFile2) filedecrypt = decryptfile(openFile2) if ende == "0": print encryptfile(fileencrypt) if ende == "1": print decryptfile(filedecrypt) </code></pre> <p>This is what I am trying to encrypt</p> <p>Sir Robin: "Oh, you liars!"</p> <p>Minstrel: [singing] "Bravely taking to his feet, he beat a very brave retreat. A brave retreat by brave Sir Robin."</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload