Python | Scramble words from a text file

File handling | Python Methods and Functions

Rules to follow:

  • Words containing no more than 3 characters do not need to be encrypted.
  • Do not encrypt the first and last characters, so scrambling can become Srbmnacilg or Srbmnailcg or Snmbracilg , that is, letters other than the first and last can encrypt in any order.
  • The punctuation at the end of a word to be kept as is, that is, “Amazing” may become “Spsirnirug” but not “Spsirn, irug”.
  • The following punctuation marks must be supported — Semicolon, Semicolon, Semicolon, Exclamation point.
  • Do this for the file and save the sequence of lines.

When the program starts, it should prompt the user for a name input file and generate ciphertext output file. The output file must be named by appending the word "Scrambled" to the input file name.

Example :

  Input:  MyFile. txt - & gt; Scrambling words is very interesting. Because even if they are scrambled, it doesn't impact our reading. Because we don't read letter by letter, we read the word as a whole.  Output:  MyFileScrambled.txt - & gt; Srbmnacilg wrods is vrey itrensientg. Bscauee even if tehy are srelabcmd, it dosn'et ipcmat our raidneg. Bacusee we dn'ot raed lteetr by letetr, we raed the wrod as a wolhe. 

Below is the implementation:

import random

 

punct = ( "." , " ; " , "! " , "?" , "," )

count = 0

new_word = ""

 

inputfile = input ( " Enter input file name: " )

 

with open (inputfile, 'r' ) as fin:

for line in fin.readlines ():  # Read line by line in a text file

 

for word in line.split ():  # Read word by word on each line

  

  if len (word) & gt;  3 # If the word length is & gt; 3

 

& # 39; & # 39; & # 39; If word ends with punctuation

Remove first letter, last letter and punctuation

Shuffle words

Add deleted letters (first letter)

  Add deleted letters (last letter)

Add deleted letters (punctuation marks) & # 39; & # 39; & # 39;

 

if word.endswith (punct):

word1 = word [ 1 : - 2 ]

word1 = random.sample (word1, len (word1))

word1.insert ( 0 , word [ 0 ])

word1.append (word [ - 2 ])

word1.append (word [ - 1 ])

 

& # 39; & # 39; & # 39; If there is no punctuation mark

Remove first letter and last letter

Shuffle word

Add deleted letters (first letter)

Add deleted letters (last letter)

Add a word and “” to the previous words “”

 

else :

  word1 = word [ 1 : - 1 ]

word1 = random.sample (word1, len (word1))

word1.insert ( 0 , word [ 0 ])

word1.append (word [ - 1 ])

new_word = new_word + '' .join (word1) + ""

 

  & # 39; & # 39; & # 39; If the word length is & lt; 3

just add the word and & quot; & quot; to the previous words & quot;

 

else :

new_word = new_word + word + ""

 

  # & quot; Add to & lt; filename & gt; Scrambled.txt & quot;

 

with open ((inputfile [: - 4 ] + "Scrambled.txt" ), 'a +' ) as fout:

fout.write (new_word + "" )

 

new_word = ""

Exit:

 Smcinrablg wodrs very Bauscee eevn tehy dnoes't icpamt Bcuesae d'not read lteter raed wrod whole. 




Tutorials