如何只更改文件中的一个参数?

2024-05-19 05:06:33 发布

您现在位置:Python中文网/ 问答频道 /正文

所以,我想更改文件中的文本莱科维.txt,但我想改变的是,每一行都是这样的:

name|2ndname|price|stock|serial|False/True|number(starting from 1 and appending by 1

每次选择要更改的数字时,我都要从False更改为True。我该怎么做?你知道吗

print("\n-- Brisanje lekova -- \n")
        print("+=============================================================================+")
        print("| Br. |    Naziv      |  Gen. Naziv  | Serijski br. | Kolicina|      Cena     |")
        print("+=============================================================================+\n")
        n = 1
        lista_lekova = open("lekovi.txt",'r').readlines()
        redovi = []
        lek={}
        for i in lista_lekova:
            deo = i.strip().split("|")
            redovi.append(deo)
            print("| ",  n ,  " | {:13} | {:12} | {:12} | {:7} | {:6}.00 din | ".format(deo[0],deo[1],deo[2],deo[3],deo[4]))    
            n = n + 1

            lek[0] = deo[0]
            lek[1]= deo[1]
            lek[2]= deo[2]
            lek[3]= deo[3]
            lek[4]= deo[4]
            lek[5]= deo[5]
            lek[6]= deo[6]
        print("+=============================================================================+")
        izbrisanilek = input("Lek pod kojim brojem zelite da izmenite:  ")
        izbrisaniLek = int(izbrisanilek)
        izbrisaniLek = izbrisaniLek - 1
        for lek in lista_lekova:
            print (lek.deo[6])
            k = "23"
            if k == izbrisaniLek:
                ceoLek = deo[0] + "|" + deo[1] + "|" + deo[2] + "|" + deo[3] + "|" + deo[4] + "|" + "True" + "|" + deo[6] + "\n"
                lekovistr = open("lekovi.txt" , "w")
                lekovistr.write(ceoLek)
                lekovistr.close()

Tags: txtfalsetrueforopenprintlistadeo
1条回答
网友
1楼 · 发布于 2024-05-19 05:06:33

我修改了你的密码。对于以下形式的输入文件:

name|2ndname|price|stock|serial|False|number(starting from 1 and appending by 1
name|2ndname|price|stock|serial|False|number(starting from 1 and appending by 1
name|2ndname|price|stock|serial|False|number(starting from 1 and appending by 1
name|2ndname|price|stock|serial|False|number(starting from 1 and appending by 1
name|2ndname|price|stock|serial|False|number(starting from 1 and appending by 1
name|2ndname|price|stock|serial|False|number(starting from 1 and appending by 1

可以在第3行中将假改为真,如下所示:

# read a file
with  open("test.txt",'r') as f:
    lista_lekova = open("test.txt",'r').readlines()

# get a line number to change    
izbrisanilek = input("Lek pod kojim brojem zelite da izmenite:  ")
izbrisaniLek = int(izbrisanilek)
izbrisaniLek = izbrisaniLek - 1


# here will be stored output lines 
out_lines = []

#iterate for each line and change izbrisaniLek line 
for k,lek in enumerate(lista_lekova): 

    # the spliting can also be avoided, just to change False to True.
    # But you seem to do some reformatting of other values.   
    deo = list(map(lambda v: v.strip(), lek.split('|'))) 

    # change False to True for the selected line.
    # it always makes it true. Not sure if you want to toggle the values, or just always have True there.
    if k == izbrisaniLek:
        deo[5] = 'True'

    ceoLek = "|".join(deo)
    #print(ceoLek)
    out_lines.append(ceoLek)

# save output files to a file
with open("lekovi_out.txt" , "w") as f:
    f.writelines(out_lines)

输出为:

name|2ndname|price|stock|serial|False|number(starting from 1 and appending by 1
name|2ndname|price|stock|serial|False|number(starting from 1 and appending by 1
name|2ndname|price|stock|serial|True|number(starting from 1 and appending by 1
name|2ndname|price|stock|serial|False|number(starting from 1 and appending by 1
name|2ndname|price|stock|serial|False|number(starting from 1 and appending by 1
name|2ndname|price|stock|serial|False|number(starting from 1 and appending by 1

希望这会有所帮助。还是不太清楚你想做什么,为什么。不需要对每一行进行迭代,只需更改一行,但是似乎您正在修改/重新格式化这些行,所以我只包含了迭代。你知道吗

相关问题 更多 >

    热门问题