Python - Scramble File
I have created the following code to scramble the letters in a word
(except for the first and last letters), but how would one scramble the
letters of the words in Text file;
input file is scramble.txt and output file is scramblewrite.txt all the contain scrable
file_in=open("scramble.txt","r")
a=file_in.read()
file_out=open("scrablewrite.txt","w")
str=a.split(' ')
file_out.write("hello")
for l in str:
if len(l)>3:
if l[-1:]=="," or l[-1:]=="!" or l[-1:]=="?" or l[-1:]==".":
s=l[1:-2]
ss=''.join(random.sample(s,len(s)))
sa=l[0]+ss+l[-2:]
file_out.write(sa+' ')
else:
s=l[1:-1]
ss=''.join(random.sample(s,len(s)))
sa=l[0]+ss+l[-1:]
file_out.write(sa+' ')
else:
file_out.write(l+' ')
file_in.close()
file_out.close()
input file is scramble.txt and output file is scramblewrite.txt all the contain scrable
Code is:
import randomfile_in=open("scramble.txt","r")
a=file_in.read()
file_out=open("scrablewrite.txt","w")
str=a.split(' ')
file_out.write("hello")
for l in str:
if len(l)>3:
if l[-1:]=="," or l[-1:]=="!" or l[-1:]=="?" or l[-1:]==".":
s=l[1:-2]
ss=''.join(random.sample(s,len(s)))
sa=l[0]+ss+l[-2:]
file_out.write(sa+' ')
else:
s=l[1:-1]
ss=''.join(random.sample(s,len(s)))
sa=l[0]+ss+l[-1:]
file_out.write(sa+' ')
else:
file_out.write(l+' ')
file_in.close()
file_out.close()
Input File :
Python is an interpreted high-level programming language for general-purpose programming. Created by Guido van Rossum and first released in 1991, Python has a design philosophy that emphasizes code readability, notably using significant whitespace. It provides constructs that enable clear programming on both small and large scales.
Output FIle:
Ptyohn is an ieterntrepd hlgv-heiel poaigmmnrrg lgnauage for geppr-aelousrne pmngarroimg. Caeretd by Giduo van Rsuosm and fsrit raeseeld in 1991, Pohytn has a dsiegn pohopshily that ehpzamesis code rdeliabtiay, nbtloay uisng sinciaginft wphtcaseie. It privodes cttnrsuocs that enbale celar pimrromgang on both samll and large saslce.
Comments
Post a Comment