views:

69

answers:

4

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()
A: 

When you read a file by lines you are implicitly splitting the file up by lines and this discards the newlines. If you want newlines you should simply append a newline manually:

enctextCC.write(curr + "\n")

Your other alternative would be to read in the lines from the first file using the readline function wich preserves the trailing newline.

Andrew Hare
Hmmm, nothing happened. I can't use the readline function because I need to manipulate each character in each line.
pythonTA
+2  A: 

you are doing it wrong

out_file = open("output.txt", "w")
for line in open("input.txt", "r"):
    out_file.write(line)
    out_file.write("\n")

Note that we don't check for newline endings because we fetch items one line at a time, so we are sure that after a line we have read follows a newline

But why do you need to do this instead of a normal copy?

Edit: in case all you need is copy a file, use this:

from shutil import copy
copy("input.txt", "output.txt")

In case you need to read whole file, use the read() function, like this:

file_data = open("input.txt", "r").read()
# manipulate the data ...
output = open("output.txt", "w")
output.write(file_data)
output.close()

EDIT 2: so, if you are trying to map other ASCII values to each character, you are doing it right, except for:

  • You forgot to write other characters, you will only output \n characters
  • Make sure you actually read the newlines, since readlines() and iterating through the file don't return newlines.

Your code will look more like this:

for j in range(len(orig[i])):
        curr = orig[i][j]
        if ord(curr) == 10:  
            enctextCC.write(curr)
        else:
            enctextCC.write(transformed(curr))
Gabi Purcaru
This is what I have except instead of transformed() I have the other if cases. The enctextCC.write(curr) for newline still isn't working though.
pythonTA
then maybe you are not reading the newlines - check that by adding a `print '\n' in orig`.
Gabi Purcaru
A: 

I'm not sure how you're getting your input...this works fine:

input = open('filename', 'r')
lines = input.readlines()
output = open('outfile', 'w')
output.writelines(lines)
Nathon
I'm making a series of ciphers which requires me to get single characters then map them to other ascii values, then write the new mapped character into a new file.
pythonTA
Should still work fine; just operate on `lines`. It includes newlines.
Nathon
+1  A: 

You can open the files in binary mode to preserve the EOL:

with open(filenameIn, 'rb') as inFile:
 with open(filenameOut, 'wb') as outFile:
  for line in inFile:
   outFile.write(line)
Sacha
I would vote for this answer except for the inexplicable call to .upper(). Why is that there? I don't see a requirement for it in the question.
Steven Rumbalski
I Removed it :)
Sacha