在Python中从一个文件到另一个文件读写新行
我试图从一个文件中读取数据,然后写入另一个文件。当我试图将新行从原始文件保留到新文件时,问题就出现了在Python中从一个文件到另一个文件读写新行,python,file-io,newline,Python,File Io,Newline,我试图从一个文件中读取数据,然后写入另一个文件。当我试图将新行从原始文件保留到新文件时,问题就出现了 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:
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()
def caesar\u加密(原版,轮班):
enctextCC=open(“CCencoded.txt”,“w”)
对于范围内的i(len(orig)):
对于范围内的j(len(orig[i]):
curr=原[i][j]
如果ord(当前)==10:
enctextCC.write(“\n”)//不起作用:(
elif ord(当前)<97:
enctextc.write(当前)
elif ord(当前)+班次>122:
enctextc.write(chr(ord(curr)+shift-26))
elif ord(curr)+shift>=97&ord(curr)+shift当您按行读取文件时,您将隐式地按行拆分文件,这将丢弃换行符。如果您想要换行符,您只需手动附加换行符:
enctextCC.write(curr + "\n")
另一种选择是使用保留尾随换行符的函数从第一个文件中读取行。您做错了
out_file = open("output.txt", "w")
for line in open("input.txt", "r"):
out_file.write(line)
out_file.write("\n")
请注意,我们不检查换行符的结尾,因为我们一次只取一行,所以我们确定在我们读过的一行之后会有一个换行符
但是为什么你需要这样做,而不是一个正常的副本
编辑:如果您只需要复制一个文件,请使用以下方法:
from shutil import copy
copy("input.txt", "output.txt")
如果需要读取整个文件,请使用read()
函数,如下所示:
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()
file_data = open("input.txt", "r").read()
# manipulate the data ...
output = open("output.txt", "w")
output.write(file_data)
output.close()
for j in range(len(orig[i])):
curr = orig[i][j]
if ord(curr) == 10:
enctextCC.write(curr)
else:
enctextCC.write(transformed(curr))
编辑2:因此,如果您试图将其他ASCII值映射到每个字符,您的操作是正确的,除了:
- 您忘记写入其他字符,将只输出
\n
字符
- 确保您实际读取了换行符,因为
readlines()
和迭代文件不会返回换行符
您的代码看起来更像这样:
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()
file_data = open("input.txt", "r").read()
# manipulate the data ...
output = open("output.txt", "w")
output.write(file_data)
output.close()
for j in range(len(orig[i])):
curr = orig[i][j]
if ord(curr) == 10:
enctextCC.write(curr)
else:
enctextCC.write(transformed(curr))
我不确定你是如何得到你的意见的……这很好:
input = open('filename', 'r')
lines = input.readlines()
output = open('outfile', 'w')
output.writelines(lines)
您可以以二进制模式打开文件以保留EOL:
with open(filenameIn, 'rb') as inFile:
with open(filenameOut, 'wb') as outFile:
for line in inFile:
outFile.write(line)
什么不起作用?怎么会起作用?你到底想做什么?看起来你只是在给新文件写换行符…我还有几个例子能起作用,只是在我试图保留换行符时它断了。你应该看看str.maketrans
和str.translate
。我正在制作一系列密码ch要求我获取单个字符,然后将它们映射到其他ascii值,然后将新映射的字符写入新文件。应该仍然可以正常工作;只需对行进行操作即可。它包括新行。嗯,什么都没有发生。我不能使用readline函数,因为我需要对每行中的每个字符进行操作。我会投票赞成这一点nswer除了对.upper()的莫名其妙的调用之外。为什么会这样?我在问题中没有看到对它的要求。这是我所拥有的,除了转换()之外,我还有其他if情况。enctextCC.write(curr)因为换行符仍然不起作用。那么可能您没有阅读换行符-通过在orig中添加一个打印“\n”来检查这一点。