In python, I need to open a text file that I have, read each character from the
ID: 3537495 • Letter: I
Question
In python, I need to open a text file that I have, read each character from the file and store it to a list. Then I need to traverse the list and determine each letter's ASCII code and output the ASCII code to an output file with one code per line. Then I need a second seperate program that coverts the values stored in the output file back to letters and store those to a second output file. It has to have 2 functions and one has to have parameters. I have been trying on this for hours now and have gotten absolutely nowhere. Thanks for any help.
Explanation / Answer
#FIRST PROGRAM
#LET THE INPUT TEXT FILE BE CALLED input.txt
#LET THE OUTPUT TEXT FILE BE CALLED output.txt
#make sure input.txt is in the same directory as this code
#function 1
def readInput():
f=open("input.txt","r")
text=f.read();
f.close();
textList=[]
for c in text:
textList.append(c)
return textList
#function 2
def convertASCII(textList):
f=open("output.txt","w")
for c in textList:
f.write(str(ord(c))+" ")
f.close()
#run both functions
input = readInput()
convertASCII(input)
#############################################################
#SECOND PROGRAM
#LET THE INPUT TEXT FILE BE THE PREVIOUS OUTPUT FILE output.txt
#LET THE OUTPUT TEXT FILE BE CALLED FinalOutput.txt
#make sure output.txt is in the same directory as this code
#function 1
def readOutput():
f=open("output.txt","r")
text=f.read();
f.close();
asciiList=text.split()
return asciiList
#function 2
def ASCIItoTEXT(asciiList):
f=open("FinalOutput.txt","w")
for c in asciiList:
f.write(chr(int(c)))
f.close()
#run both functions
ascii = readOutput()
ASCIItoTEXT(ascii)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.