开发者

Reading and Writing a new line from a file to another in Python

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 &l开发者_如何学编程t;= 122  :
            enctextCC.write(chr(ord(curr)+shift))
    enctextCC.write("\n")

enctextCC.close()


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))


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)


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.


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)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜