【问题标题】:How to change only 1 parameter in file?如何仅更改文件中的 1 个参数?
【发布时间】:2015-03-21 23:27:26
【问题描述】:

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

【问题讨论】:

  • 所以你的文件 lekovi.txt 只有一行?
  • 不,它有多行,这就是为什么我输入数字以便我可以选择它,但不知道如何......
  • 那么究竟是什么问题呢?有什么错误吗?预期的输出是什么?
  • 预期输出是从 False 更改为 True 为所选项目(行)并将其写入文件
  • 但是在你的文件中你有 True 和 False 即 (False/True)。你想删除其中之一吗?请具体。你的问题不清楚。

标签: python arrays string list file


【解决方案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 更改为 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

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

【讨论】:

    猜你喜欢
    • 2020-04-24
    • 2023-03-22
    • 2017-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-02
    • 2020-11-12
    相关资源
    最近更新 更多