0
with open(sys.argv[2]) as f:
     processlist = f.readlines()
     for a in range(0,1):
         process = processlist[a]
         print process
         for b in range(0,3):
             process1 = process.split()
             print process1[b]

sys.argy[2 ] files just has 2 sentences

Sunday Monday
local owner public

I am trying to read once sentence at a time and in each sentence I am trying to access one word at a time.... I am able to get the things i need individually but the loop doesn’t not iterate... it stops after first iteration....

2 Answers 2

3
with open(sys.argv[2]) as f:
    for line in f: #iterate over each line
        #print("-"*10) just for demo
        for word in line.rstrip().split(): #remove \n then split by space
            print(word)

Over your file would produce

----------
Sunday
Monday
----------
local
owner
public
Sign up to request clarification or add additional context in comments.

Comments

2

To answer your question why the loop doesn't iterate:

range(0,1)

contains only the element 0, since the upper bound is not included in the result. Similarly,

range(0,5)

would, when viewed as a list, be [0,1,2,3,4].

The correct way to iterate over a file is demonstrated by @HennyH's answer.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.