Как отредактировать определенное c текстовое значение в текстовом файле - PullRequest
0 голосов
/ 28 мая 2020

Я хочу отредактировать / обновить конкретное значение c в текстовом файле. Но в моем коде он просто добавляет значение из ввода пользователя в текстовый файл и не обновляет его вообще.

Вот мой текстовый файл. Он состоит из (номер сотрудника Фамилия, имя Должность, дата рождения в отделе)

 123456789, Jane, Jane, Manager, ADMIN, 1/1/2000, 1000;
 332244556, Dane, John, Manager, ADMIN, 1/2/1999, 1000;
 234567890, Doe, Jane, Manager, ADMIN, 1/2/1999, 1000;

Вот мой код

def updates():
    employee_num = []
    last_name = []
    first_name = []
    emp_possition=[]
    emp_department=[]
    emp_birthdate=[]
    emp_rate = []
    with open("empRecord.txt", 'r+') as files:
        for info in files:
             info = info.strip()
             if len(info) >= 1:
                lists = info.split(',')
                employee_num.append(lists[0].strip())
                first_name.append(lists[1].strip())
                last_name.append(lists[2].strip())
                emp_possition.append(lists[3].strip())
                emp_department.append(lists[4].strip())
                emp_birthdate.append(lists[5].strip())
                emp_rate.append(lists[6].rstrip(';'))


        y = input("Enter Employee number you wish to update Records  ")
        index = employee_num.index(y)
        print('Employee', y + "'s", "Position is:", emp_possition[index])
        changes = input("Enter the new Position of the employee")
        #it just add the input and it does not change the text file
        files.write(f"{changes}")

updates()
...