【问题标题】:Reading and Writing a new line from a file to another in Python在 Python 中从一个文件读取和写入一个新行到另一个文件
【发布时间】:2010-10-15 18:29:07
【问题描述】:

我正在尝试从一个文件中读取并写入另一个文件。当我试图将原始文件中的换行符保留到新文件时,就会出现问题。

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

关于哪里出了问题有什么建议吗?

谢谢

编辑: 解决方案是在外部 for 循环的末尾添加换行符。由于我正在阅读列表列表,因此内部循环基本上是一行。所以它应该是这样的:

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

【问题讨论】:

  • 什么不起作用?怎么会,你到底想做什么?
  • 看起来您只是在新文件中写入换行符...
  • 我还有几个可以工作的案例,只是当我试图保留它打破的换行符时。
  • 你应该看看str.maketransstr.translate

标签: python file-io newline


【解决方案1】:

你做错了

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() 函数,如下所示:

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

编辑 2:因此,如果您尝试将其他 ASCII 值映射到每个字符,那么您做对了,除了:

  • 你忘了写其他字符,你只会输出\n个字符
  • 确保您确实阅读换行符,因为readlines() 和遍历文件不会返回换行符。

你的代码看起来更像这样:

for j in range(len(orig[i])):
        curr = orig[i][j]
        if ord(curr) == 10:  
            enctextCC.write(curr)
        else:
            enctextCC.write(transformed(curr))

【讨论】:

  • 这是我所拥有的,除了 transform() 我有其他 if 案例。换行符的 enctextCC.write(curr) 仍然无法正常工作。
  • 那么您可能没有阅读换行符 - 通过添加 print '\n' in orig 来检查。
【解决方案2】:

您可以以二进制模式打开文件以保留 EOL:

with open(filenameIn, 'rb') as inFile:
 with open(filenameOut, 'wb') as outFile:
  for line in inFile:
   outFile.write(line)

【讨论】:

  • 我会投票给这个答案,除了莫名其妙地调用 .upper()。为什么会在那里?我在问题中没有看到对它的要求。
【解决方案3】:

当您按行读取文件时,您会隐式地按行拆分文件,这会丢弃换行符。如果你想要换行,你应该手动添加一个换行:

enctextCC.write(curr + "\n")

您的另一种选择是使用 readline 函数从第一个文件中读取行,该函数保留尾随换行符。

【讨论】:

  • 嗯,什么也没发生。我不能使用 readline 函数,因为我需要操作每一行中的每个字符。
【解决方案4】:

我不确定你是如何得到你的意见的……这很好用:

input = open('filename', 'r')
lines = input.readlines()
output = open('outfile', 'w')
output.writelines(lines)

【讨论】:

  • 我正在制作一系列密码,这需要我获取单个字符,然后将它们映射到其他 ascii 值,然后将新映射的字符写入新文件。
  • 应该仍然可以正常工作;只需在lines 上操作即可。它包括换行符。
猜你喜欢
  • 2021-01-23
  • 2018-10-30
  • 1970-01-01
  • 2012-10-22
  • 1970-01-01
  • 2023-04-05
  • 1970-01-01
  • 2016-10-18
  • 2013-10-26
相关资源
最近更新 更多