f = open("Trades.txt","r")
writer = open("trading.txt","w")
options = input("A:Check trades for 1 or more players/B:Check trades between 2 players: ").lower()
if options == 'b':
player1 = input("Enter the name of player 1: ")
player2 = input("Enter the name of player 2: ")
for lines in f:
if player1 and player2 in lines:
writer.write(lines)
文本文件看起來是這樣的:寫從文件中的所有行到一個文件中,直到達到一個特定的字符串
=======================
[time] player trading with playerx
(To: player, From: playerx)
item
=======================
[Jan 13, 2016 11:53:49 PM Central European Time] y trading with x
(To: x, From: y)
item
=======================
用戶將被要求輸入2名在文本文件中找到。
這兩個名字必須在我已經完成的文本中找到。
然後,具有名稱的行後面的行將不得不寫入文件,直到達到「=======================」。
因此,寫入的數據會看起來像:
[time] player trading with playerx
(To: player, From: playerx)
item
在此先感謝
PS名稱後的行數會不同,所以不能像寫後線的一組量匹配
Stack Overflow是不是免費的代碼編寫的服務。請向我們展示您解決此問題的嘗試,並精確詢問您遇到的問題。 –
我有嗎?看看第一位 – I3uzzzzzz
可能重複[如何測試一個變量對多個值?](http://stackoverflow.com/questions/15112125/how-do-i-test-one-variable-against-多值) – MattDMo