[英]How do I remove the square brackets from the list? By using the strip function
calendar = []
f = open("calendar.txt","w")
while True:
choice = input ("enter a,b,c,d or e")
if choice == "e":
print ("you have quitted this program")
break #to quit the entire program
if choice == "a":
newevent = input ("enter the name of an event you want to add")
calendar.append ([newevent])
y= int(input("enter the year of your event"))
m = int(input("enter the month of your event"))
d = int(input("enter the day of your event"))
h = int(input("enter the hour of your event"))
mi = int(input("enter the minute of your event"))
date = (y,m,d,h,mi)
for i in range (0,len(calendar)):
f = open("calendar.txt","r")
st = f.readlines()
for y in range (0,len(st)):
st[y] = st[y].strip("\n")
st[y]= st[y].split(",")
st[y][1]=int(st[y][1])
f.close()
for x in range (0,len(calendar)):
if calendar[x][0]== newevent:
calendar[x].append (date)
print (calendar)
这是我的 output:
enter a,b,c,d or ea
enter the name of an event you want to addswim
enter the year of your event2020
enter the month of your event2
enter the day of your event2
enter the hour of your event2
enter the minute of your event2
[['swim', (2020, 2, 2, 2, 2)]]
enter a,b,c,d or e
如何删除它周围的方括号?
print(",".join(list))
或者
print(str(list)[1:-1])
您只有一个列表内的列表。 这是正常的,因为日历可以显示为事件列表,每个事件由一个名称和一个日期组成,可以存储为列表。
要打印您添加到日历中的最后一个事件,您可以使用print(calendar[-1])
。
它们是您程序中的一些问题:
f
的形式以写入模式打开日历,然后以f
的形式以读取模式重新打开它,而无需对其调用close()
。为了使您的程序更简单:
这是您的程序的重构版本:
while True:
choice = input("Enter Q to quit, A to add a new event : ").upper()
if choice == 'Q':
print ("You have quitted this program")
break #to quit the entire program
elif choice == 'A':
name = input ("Enter the name of an event you want to add : ")
y= int(input("Enter the year of your event: "))
m = int(input("Enter the month of your event: "))
d = int(input("Enter the day of your event: "))
h = int(input("Enter the hour of your event: "))
mi = int(input("Enter the minute of your event: "))
date = (y,m,d,h,mi)
# Opening the file in append mode
f = open("calendar.txt", 'a', encoding='utf-8')
f.write('{},{},{},{},{},{}\n'.format(name, date[0], date[1], date[2], date[3], date[4]))
f.close()
print("------ ALL YOUR EVENTS ------")
# Showing the entire calendar
f = open("calendar.txt","r")
for event in f.readlines():
split = event.split(',')
print("{} happens on {}/{}/{} at {}:{}".format(split[0],
int(split[1]),
int(split[2]),
int(split[3]),
int(split[4]),
int(split[5])
))
print('-----------------------------')
和 output:
Enter Q to quit, A to add a new event : A
Enter the name of an event you want to add : Christmass
Enter the year of your event: 2020
Enter the month of your event: 12
Enter the day of your event: 25
Enter the hour of your event: 0
Enter the minute of your event: 0
Christmass,(2020, 12, 25, 0, 0)
My birthday,(1997, 1, 1, 6, 45)
C:\Users\tcravic\Downloads>python test.py
C:\Users\tcravic\Downloads>python test.py
Enter Q to quit, A to add a new event : A
Enter the name of an event you want to add : Christmass
Enter the year of your event: 2020
Enter the month of your event: 12
Enter the day of your event: 25
Enter the hour of your event: 0
Enter the minute of your event: 0
------ ALL YOUR EVENTS ------
Christmass happens on 2020/12/25 at 0:0
-----------------------------
Enter Q to quit, A to add a new event : A
Enter the name of an event you want to add : My Birthday
Enter the year of your event: 1997
Enter the month of your event: 03
Enter the day of your event: 01
Enter the hour of your event: 6
Enter the minute of your event: 45
------ ALL YOUR EVENTS ------
Christmass happens on 2020/12/25 at 0:0
My Birthday happens on 1997/3/1 at 6:45
-----------------------------
Enter Q to quit, A to add a new event : q
You have quitted this program
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.