0

Here is the code

f = open("mmk.tsv")
g = f
for a in f:
    print("-")
    for b in g:
        print("+")

The output is only one "-" and several "+"s.

When I remove the inner loop the outer loop works as expected.

  • You need to be more specific about the problem you're facing, so that the community can help you. In this case, are you trying to parse a tab-seperated file? – a'r May 16 '20 at 10:59

1 Answers1

1

You can only iterate over a file once. Doing it through different variables doesn't change that.

Read file file into a list, then you can have multiple iterations.

with open("mmk.tsv") as f:
    lines = f.readlines()
for a in lines:
    print("-")
    for b in lines:
        print("+")
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • _You can only iterate over a file once. Doing it through different variables doesn't change that_ it's just confusing – Kurohige May 16 '20 at 11:06