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