SUBHABRATA
SUBHABRATA

Reputation: 121

Storing iteration values

I have this code:

  file_open=open("/python32/doc1.txt","r")
  file=a1.read().lower()
  for line in file:
       line_word=line.split()

This works fine. But if I print line_word it would be printed continuously. I like to store in some variable, so that I may print a line of my choice and manipulate them at my choice. Is there any way to solve this problem?

Generally I append to a list and do the necessary stuff, but for bigger files it is really problematic.

If anyone can kindly suggest a solution.

Upvotes: 0

Views: 91

Answers (2)

stranac
stranac

Reputation: 28226

You don't need to call .read() to iterate over the lines of a file.

with open("/python32/doc1.txt", "r") as f:
    for line in f:
        line_word = line.lower().split()

Upvotes: 2

Marco de Wit
Marco de Wit

Reputation: 2804

Does this help?

file_open=open("/python32/doc1.txt","r")
file=a1.read().lower()
line_word = []
for line in file.splitlines():
    line_word.append(line.split())

Upvotes: 0

Related Questions