In txt file, the data is written like this:
- A1|Auditorium1|Prote G
- A2|Auditorium1|Prote G
A1 stands for label of auditorium, Auditorium1 stands for description, Prote G stands for address. Now, I want to make function for replacing label of auditorium with new one. I wanted to use the inputs:
def label_change():
label_old = input("Enter label u wanna change: ")
label_new = input("Enter new one: ")
with open("aud.txt","r") as f1:
for i in f1.readlines():
r = i.split("|")
label = r[0]
desc = r[1]
adr = r[2]
if label_old == label:
label_new = label
#now not sure how to continue
For example, If I input A1 as an old label and A5 as a new label, can I somehow replace A1 with A5 without deleting or changing description and address in my file? After this I tried opening file in 'w' mode, so i can write, but it deletes every line and 'a' mode adds whole line instead of changing wanted label. Is there any easier method?
label,desc,adr = i.split("|")- will throw error if your lines are malformed - same as your solution