Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 更改文件中的整行_Python_List_File - Fatal编程技术网

Python 更改文件中的整行

Python 更改文件中的整行,python,list,file,Python,List,File,我的程序必须按第一个参数(312621)在文件中取一行,并更改一行或两行参数。之后必须再次写入文件。第一个参数是唯一的,不可更改 文件优先于任何内容: 我现在的代码: key = input("key >>") entitet = open("komad_namestaja.txt", "r+") allDATA = entitet.readlines() entitet.close() for line in allDATA: lineLST = line.split("

我的程序必须按第一个参数(312621)在文件中取一行,并更改一行或两行参数。之后必须再次写入文件。第一个参数是唯一的,不可更改

文件优先于任何内容:

我现在的代码:

key = input("key >>")

entitet = open("komad_namestaja.txt", "r+")
allDATA = entitet.readlines()
entitet.close()
for line in allDATA:
    lineLST = line.split("|")

    while key == lineLST[0]:

        print("name", lineLST[1])
        name = input("name >> ")
        if name == "":
            name = lineLST[1]

        print("color", lineLST[2])
        color = input("color >> ")
        if color == "":
            color = lineLST[2]
按键输入:312

名称输入:cokoloko

颜色输入:蓝色

在以下日期后存档:


问题:如何用前一行重写新输入,而不丢失第二行。

尝试查看fileinput模块

for line in fileinput.input("input_file.txt"):
    if check(line):
       print line.replace("red","blue")
    else:
       print line

这不能帮助我,我必须改变整个线路而不丢失之前或之后的线路
312|cokoloko|blue
621|digimon tabure|black
for line in fileinput.input("input_file.txt"):
    if check(line):
       print line.replace("red","blue")
    else:
       print line