如何从 txt 中排序数据?
How can i sort data from a txt?
我需要按时间顺序对这些数据进行排序,希望按日期排序,然后按时间排序,但现在我只想按日期排序...信息在 TXT 文件中:
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
这是我现在的代码:
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()
所以在 def print_appointments():
中,我尝试对数据进行排序,这是我最后一次尝试,None 我所做的那些给了我一个适度的积极结果。
你有一些错误:
- 无文件扩展名
open("sort_appointments"
- 按日期排序剩余按行拆分文件
- 将列表写入文件时,需要打开后追加“a”
- 读取文件时,必须输入字母“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()
我需要按时间顺序对这些数据进行排序,希望按日期排序,然后按时间排序,但现在我只想按日期排序...信息在 TXT 文件中:
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
这是我现在的代码:
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()
所以在 def print_appointments():
中,我尝试对数据进行排序,这是我最后一次尝试,None 我所做的那些给了我一个适度的积极结果。
你有一些错误:
- 无文件扩展名
open("sort_appointments"
- 按日期排序剩余按行拆分文件
- 将列表写入文件时,需要打开后追加“a”
- 读取文件时,必须输入字母“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()