如何在文件中只更改1个参数?

时间:2015-01-23 00:33:41

标签: python arrays string list file

所以,我想更改文件lekovi.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()

1 个答案:

答案 0 :(得分: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
name|2ndname|price|stock|serial|False|number(starting from 1 and appending by 1

可以在第3行更改False to True,如下所示:

# 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

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