简体   繁体   English

替换文本文件中列表中的文本

[英]Replacing a text in list within textfile

I'm currently doing an assignment where I have to make an Apartment Registering program. 我目前正在做一项作业,必须做一个公寓登记程序。 I saved the apartment info like this in a textfile: 我将这样的公寓信息保存在一个文本文件中:

['B01','3','Master: TP_1','TP_2','TP_3']
['B02','2','Master: TP_1','TP_2','TP_3']
['B03','1','Master: TP_1','TP_2','TP_3']
['B04','0','Master: TP_1','TP_2','TP_3']
['B05','1','Master: TP_1','TP_2','TP_3']
['B06','2','Master: TP_1','TP_2','TP_3']
['B07','3','Master: TP_1','TP_2','TP_3']
['B08','2','Master: TP_1','TP_2','TP_3']
['B09','1','Master: TP_1','TP_2','TP_3']
['B10','0','Master: TP_1','TP_2','TP_3']

Now, whenever a new student register to the apartment, I would like to add 1 to the number of occupants, which is the second value in the list. 现在,每当有新学生注册到公寓时,我想在入住人数上加1,这是列表中的第二个值。 I would also like to replace the TP with student ID number. 我也想用学生证号代替TP。

def apartmentRegister(): #REGISTERING APARTMENT
    AorB=input("Would you like to register into Apartment A or B? (A/B)")
    if AorB==("A") or AorB==("a"):
        room=input("Insert room number: ")
        f=open("Apartment_A.txt", "r")
        for line in f.readlines():
            if not line or line =="\n":
                continue
            A_register=literal_eval(line)
            if A_register[-5]==(room):
                if A_register[-3]==("TP_1"):
                    f=open("Apartment_A.txt", "a")
                    TPagain=input("Please input your TP number: ")
                    new=line.replace("TP_1",TPagain)
                    f.write(new+"\n")
                    print("You're all set")
                f.close()
    elif AorB==("B") or AorB==("b"):
        room=input("Insert room number: ")
        g=open("Apartment_B.txt", "r")
        for line in g.readlines():
            if not line or line =="\n":
                continue
            B_register=literal_eval(line)
            if B_register[-5]==(room):
                if B_register[-3]==("Master: TP_1"):
                    g=open("Apartment_B.txt", "a")
                    TPagain=input("Please input your TP number: ")
                    new=line.replace("TP_1",TPagain)
                    g.write(new+"\n")
                    print("You're all set")
                g.close()

The whole code works... just the fact that I couldn't replace the values/text that I want to replace... Instead it adds a new line in the text file instead. 整个代码都起作用...只是我无法替换要替换的值/文本这一事实...相反,它在文本文件中添加了新行。

You probably want something along these lines: 您可能需要遵循以下原则:

# This is the content of the file after processing
new_contents_A = []

# Read in all lines and save them to the list
with open("Apartment_A.txt", "r") as f:
    for line in f.readlines():
        if not line or line =="\n":
            new_contents_A.append(line)
            continue
        A_register=literal_eval(line)
        if A_register[-5]==room and A_register[-3]==("TP_1"):
            TPagain=input("Please input your TP number: ")
            new=line.replace("TP_1",TPagain)
            new_contents_A.append(new)
        else:
            new_contents_A.append(line)

# Override the file with the now changed data
with open("Apartment_A.txt", "w") as f:
    for line in new_contents_A:
        f.write(line)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM