[英]Rewriting Certain parts of a text file (dynamically)
我有一個像這樣的文本文件
A,OXXX#XXXX#XXOO
B,OOXX#XXXX#XXOO
C,OXXX#XXXO#OXXO
D,OOXX#XXXX#XXOO
E,OOOX#OXXO#XXOO
F,##XX#OXXX#XO##
它有點像電影院,其中 O 是可用座位,X 是不可用座位,# 是一堵牆
bookSeat = input('Enter seats to book: ').split(',')
for booking in bookSeat:
bookRow = booking[0]
bookColumn = booking[1:]
availableSeats = seatingPlan[bookRow][int(bookColumn)-1]
if availableSeats == 'O':
f = open('Monkey Goes East-202209081430.txt','w')
#idk whats the next step
elif availableSeats == 'X':
print("Seats are already booked!')
elif availableSeats == '#':
print('These seats are unavailable')
所以如果我輸入A1,B14,它會將文本文件更新為
A,**X**XXX#XXXX#XXOO
B,OOXX#XXXX#XXO**X**
C,OXXX#XXXO#OXXO
D,OOXX#XXXX#XXOO
E,OOOX#OXXO#XXOO
F,##XX#OXXX#XO##
加粗顯示它的變化
請幫忙
假設seatingPlan
是 python 字典,例如{'A': ['X','#','O', ...]}
並假設您可以修改seatingPlan
,您可以執行以下操作:
這是偽代碼。 我沒有測試它。
bookSeat = input('Enter seats to book: ').split(',')
for booking in bookSeat:
bookRow = booking[0]
bookColumn = booking[1:]
availableSeats = seatingPlan[bookRow][int(bookColumn)-1]
if availableSeats == 'O':
seatingPlan[bookRow][int(bookColumn)-1] = 'X'
elif availableSeats == 'X':
print('Seats are already booked!')
elif availableSeats == '#':
print('These seats are unavailable')
# further checking logic ....
with open('Monkey Goes East-202209081430.txt','w') as f:
for rowCode in sorted(seatingPlan.keys()):
f.write(rowCode + ',' + "".join(seatingPlan[rowCode]) + '\n')
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.