Home > Mobile >  How can i sort data from a txt?
How can i sort data from a txt?

Time:06-01

I need to sort this data chronologically, hopefully, by date and them by time, but right now I just want to sort it by date... the information is on a TXT file:

2022/5/10 at 10 the client Mari has appointment with the Dra. Windrunner
2022/1/5 at 2 the client Ian has appointment with the Dr. Stark
2022/1/4 at 10 the client Amy has appointment with the Dra. Windrunner
2022/1/5 at 2 the client Josh has appointment with the Dr. Stark
2022/2/22 at 5 the client Mike has appointment with the Dr. Pool
2022/2/22 at 4 the client Pedro has appointment with the Dr. Stark

This is my code right now:

Docs = ("Dr. Stark", "Dra. Windrunner", "Dr. Pool")
x = 0
loop = False

DocsConverter = {
    "0" : "Dr. Stark",
    "1" : "Dra. Windrunner",
    "2" : "Dr. Pool",
    "dr. stark": "Dr. Stark",
    "dra. windrunner" : "Dra. Windrunner",
    "dr. pool" : "Dr. Pool",
    "stark" : "Dr. Stark",
    "windrunner" : "Dra. Windrunner",
    "pool" : "Dr. Pool"
}

with open("appointment_hospital.txt", "a") as file_ap:
    pass


def menu():
    option = input("select a option 1. new appointment 2.show appointments 3. Exit: (1/2/3)\n")
    if option == "1":
        new_appointment()
    elif option == "2":
        print_appointments()
    elif option == "3":
        file_ap.close()
        exit()
    else:
        print("Wrong option")


def new_appointment():
    global x
    name_client = input("Enter the name of the client:\n")
    schedule_day = input("Enter the year, month and the day of the appointment:(Y/M/D)\n")
    schedule_time= input("Enter at what hour is the appointment:\n")
    while x != 3:
        print(f"{Docs[x]}, id = {x}")
        x  = 1
    x = 0
    which_doc = input("Enter the name or the Id of the doctor: ")
    appointment_info = f"{schedule_day} at {schedule_time} the client {name_client} has appointment with the " \
                       f"{DocsConverter.get(which_doc)}\n"
    with open("appointment_hospital.txt", "a") as file:
        file.write(appointment_info)

#this is where i tried to sort the information in the txt
def print_appointments():
    with open("appointment_hospital.txt", "r") as file:
        lines_appointments = []
        for line in file:
            temp = line.split()
            for i in temp:
                lines_appointments.append(i)
        lines_appointments.sort()
        with open("sort_appointments", "w") as sort_file:
            for i in lines_appointments:
                sort_file.writelines(i)
                sort_file.writelines(" ")
            sort_file.close()
    with open("sort_appointments", "w") as sort_file:
        read_appointments = sort_file.read()
        print(read_appointments)


while not loop:
    menu()

So in def print_appointments(): I tried to sort the data, and this was my last tried, None of the ones I have done have given me a moderately positive result.

CodePudding user response:

You have some mistakes:

  1. no file extension open("sort_appointments"
  2. to sort by dates residual split the file by lines
  3. when writing a list to a file, you need to open it for appending "a"
  4. when reading a file, you must put the letter "r"
def print_appointments():
    with open("appointment_hospital.txt", "r") as file:
        lines_appointments = []
        for line in file:
            lines_appointments.append(line)
    lines_appointments.sort()

    with open("sort_appointments.txt", "a") as sort_file:
            sort_file.writelines(lines_appointments)


    with open("sort_appointments.txt", "r") as sort_file:
        read_appointments = sort_file.read()
        print(read_appointments)



print_appointments()
  • Related