[英]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.