I'm trying to read from a file and write into another. The problem arises when I'm trying to preserve newlines from the original file to the new one.
def caesar_encrypt(orig , shift):
enctextCC = open("CCencoded.txt" , 'w')
for i in range(len(orig)):
for j in range(len(orig[i])):
curr = orig[i][j]
if ord(curr) == 10:
enctextCC.write("\n") //doesn't work :(
elif ord(curr) < 97:
enctextCC.write(curr)
elif ord(curr)+shift > 122:
enctextCC.write(chr(ord(curr)+shift-26))
elif ord(curr)+shift >= 97 & ord(curr)+ shift <= 122 :
enctextCC.write(chr(ord(curr)+shift))
enctextCC.close()
Any suggestions as to what is going wrong?
Thanks
EDIT: The solution is to add the newline in at the end of the outer for loop. Since I'm reading a list of lists, the inner loop is basically a single line. So it should looks like this:
def caesar_encrypt(orig , shift):
enctextCC = open("CCencoded.txt" , 'w')
for i in range(len(orig)):
for j in range(len(orig[i])):
curr = orig[i][j]
if ord(curr) < 97:
enctextCC.write(curr)
elif ord(curr)+shift > 122:
enctextCC.write(chr(ord(curr)+shift-26))
elif ord(curr)+shift >= 97 & ord(curr)+ shift <= 122 :
enctextCC.write(chr(ord(curr)+shift))
enctextCC.write("\n")
enctextCC.close()