-1
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)

Text file looks something like this:

=======================
[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
=======================

The user will be asked to enter 2 names to find in the text file.

These 2 names will have to be found in the text, which I have done.

Then, the lines AFTER the line with the names will have to written to a file UNTIL it reaches "=======================".

So the written data would look something like:

[time] player trading with playerx
(To: player, From: playerx)
item

Thanks in advance

P.S The number of lines after the names will vary so can't write like a set amount of lines after the match

I3uzzzzzz
  • 1
  • 5
  • Stack Overflow is not a free code-writing service. Please show us your attempts at tackling this problem and ask precisely about issues you're encountering. – Łukasz Rogalski Jan 23 '16 at 18:48
  • I have? Take a look at the first bit – I3uzzzzzz Jan 23 '16 at 18:48
  • Possible duplicate of [How do I test one variable against multiple values?](http://stackoverflow.com/questions/15112125/how-do-i-test-one-variable-against-multiple-values) – MattDMo Jan 23 '16 at 18:55
  • That has nothing to do with reading a file and writing until it reaches something... – I3uzzzzzz Jan 23 '16 at 18:59

2 Answers2

0

two things to fix

first

if player1 and player2 in lines:

is wrong, probably this is better

  if player1 in lines and player2 in lines:

To stop when the ======================= is found in a for loop something like

lines = lines.strip()
if lines == '=======================':
  break

The "strip()" function removes the newlines from the input file so an exact match on the line works correctly

Just to make it clear the code

for lines in f:
    if player1 and player2 in lines:
        writer.write(lines)

is replaced with

for lines in f:
  if player1 in lines and player2 in lines:
    lines = lines.strip()
    if lines == '=======================':
      break
  writer.write(lines)
Vorsprung
  • 32,923
  • 5
  • 39
  • 63
0

Since source file object is an iterator, you may consume it anywhere, not only in for line in f statement.

To consume iterable until condition occurs you may use itertools.takewhile function.

Make an iterator that returns elements from the iterable as long as the predicate is true.

takewhile will create new iterable, but as a side effect source iterable will be consumed. In file object case - it'll read lines and move internal file pointer. After exhausting takewhile iterator, internal file pointer would point to first line after ===.

Code would be similar to something like:

import itertools

player1 = "x"
player2 = "y"
with open("Trades.txt") as src, open("trading.txt", "w") as dst:
    for src_line in src:
        if player1 in src_line and player2 in src_line:
            dst.write(src_line)
            for line in itertools.takewhile(lambda s: not s.startswith("======================="), src):
                dst.write(line)
Łukasz Rogalski
  • 22,092
  • 8
  • 59
  • 93