【问题标题】:overwriting a specific line in a txt file覆盖 txt 文件中的特定行
【发布时间】:2021-07-19 00:49:08
【问题描述】:

我还是 python 新手,我已经尝试过这种方式来覆盖 txt 文件中的一行

'''

answer =input("R/S/L/M:")

if answer == "R":
    print ("Racer")
    f=open('character.txt', 'w+')
    with open("character.txt") as file_in:
        lines = [1]
        for line in file_in:
            lines.append(line)
    amount=int(input("please enter the amount"))            
    f.write(answer )
    f.write("" + "-" + str(amount) + '\n\n')
    f.write("" + '\n')
    f.close()
elif answer == "S":
    print ("Sage")      
    f=open('character.txt', 'w+')
    with open("character.txt") as file_in:
        lines = [3]
        for line in file_in:
            lines.append(line)
    amount=int(input("please enter the amount"))            
    f.write(answer )
    f.write("" + "-" + str(amount) + '\n\n')
    f.write("" + '\n')
    f.close()

'''

它会替换所有文本行,无论 txt 文件如何 我要做的是,当我选择 R 时,它会写入 txt 文件的第一行,而当我选择 S 时,它会写入 txt 文件的第三行

我已经试过了

'''

  if answer == "R":
    print ("Racer")
    f=open('character.txt', 'w+')
    with open("character.txt", "r+") as myfile:
        data = myfile.read()
        myfile.seek(0)
        amount=int(input("please enter the amount"))
        newData1=(answer + "" + "-" + str(amount) + '\n\n')
        myfile.write(newData1)
        myfile.truncate()
    f.close()

 elif answer == "S":
    print ("Sage")       
    f=open('character.txt', 'w+')
    with open("character.txt", "r+") as myfile:
        data = myfile.read(4)
        myfile.seek(4)
        amount=int(input("please enter the amount"))
        newData2=(answer + "" + "-" + str(amount) + '\n\n')
        myfile.write(newData)
        myfile.truncate(4)
    f.close()

'''

谁能指点我正确的方向

【问题讨论】:

  • 您到底想达到什么目的?每次放置新值时,程序都会覆盖 .txt 文件中的行。您是否试图找到不覆盖文件的方法?
  • 嗨 - 请您提供您正在寻找的文件输出示例,以及代码当前正在执行的操作的示例。
  • @tamerjar 我正在尝试拥有它,所以当我选择 S 或 R 并且当我输入所选数量时,它将覆盖它们所在的当前行并保留其他行未编辑这方面的一个例子是 txt 文档 S 位于第 3 行,所以当我在程序中选择 S 并输入金额时,它会用新的金额覆盖第 3 行
  • @Cerberton 目前我正在尝试获取它,所以当我选择 s 作为选项并选择我要添加的数量时,例如 S,10,我希望它覆盖第 3 行txt 文件,但是发生了什么,当我在此程序中选择 S 或其他选项时,它会覆盖 txt 文件中的所有数据。

标签: python python-3.x txt


【解决方案1】:

您的第一种方法已经接近解决方案,但是您没有选择第一行或第三行,您只是创建一个包含 1 (lines = [1] 的列表。您首先需要打开文件。然后,您可以使用file.readlines,它返回一个列表,其中包含每一行作为一个单独的字符串,而不是file.read,它将整个文件作为一个字符串返回。然后,你为一个特定的行分配一个新值,再次打开同一个文件,但是现在处于写入模式,使用file.writelines 将行列表写回文件,就是这样!

if answer == "R":
    print("Racer")
    with open("character.txt", "r") as f: # with automatically closes the file - no need to do so yourself
        lines = f.readlines()

    amount = input("please enter the amount") # no need to convert the amount to int first if you then convert it back  to string below       
    lines[0] = answer + "-" + amount
    with open("character.txt", "w") as f:
        f.writelines(lines)

我会让你自己调整其他答案的代码。请注意,如果您将上面的代码放在一个接受数字(要覆盖的行)和字符串(答案)的函数中,则可以节省很多代码行。

您一定想知道我为什么将lines[0] = ... 放在代码中。好吧,我们从 1 开始计数,但 Python 从 0 开始计数!你所说的第 1 行是 Python 的第 0 行,你所说的第 2 行是 Python 的第 1 行。等等。

这里有一些关于你的第二种方法的注释:file.seek seeks to the byte 在你传递给它的数字指定的文件中,不是到该行。 file.truncate 调整大小文件。它确实not overwrite 文件。

【讨论】:

  • 谢谢你,我现在知道我哪里出错了 这很有帮助,谢谢你写出解释,它真的帮助我更好地理解它
猜你喜欢
  • 2019-05-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-23
  • 1970-01-01
  • 2015-05-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多