[英]Rewriting Certain parts of a text file (dynamically)
I have a text file like this我有一个像这样的文本文件
A,OXXX#XXXX#XXOO
B,OOXX#XXXX#XXOO
C,OXXX#XXXO#OXXO
D,OOXX#XXXX#XXOO
E,OOOX#OXXO#XXOO
F,##XX#OXXX#XO##
Its something like a movie cinema Where O is available seats, X is unavailable seats and # is a wall它有点像电影院,其中 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')
So if i input A1,B14, it will update the text file into所以如果我输入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##
Bolded to show its been change加粗显示它的变化
please help请帮忙
assuming seatingPlan
is a python dict like {'A': ['X','#','O', ...]}
and assuming you can modify seatingPlan
you could do something along the lines of:假设seatingPlan
是 python 字典,例如{'A': ['X','#','O', ...]}
并假设您可以修改seatingPlan
,您可以执行以下操作:
this is pseudo-code.这是伪代码。 i did not test it.我没有测试它。
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.