Terry Green wrote:

def main():
    pass

Useless function, does nothing. Why is it here?

if __name__ == '__main__':
    main()

Also does nothing useful. Why is it here?

postPos=words[3]

This like will fail with NameError, because words is not defined anywhere.

This is not the code you are running. We can't tell what is wrong with the code you run when you show us something completely different.

Please send us the *actual* code you run, not something you've re-typed from memory, or copied and pasted in bits and pieces from different places.

By the way, why are you calling a *word* "postPos"? Pos normally is an abbreviation for *position*, which will be a number. To say:

postPos = words[3]

makes postPos a word, not a position. So the name is misleading. You should change it.



f = open ("c:/users/terry/downloads/tup1012k/tup1012x.drf","r")
lines = f.readlines()
for line in lines:
    words = line.split(",")
    print (words[3],postPos)

close = f

I'm pretty sure that's not what you want. This line "close = f" will define a new variable called "close", which is equal to the open file object "f".

What you want is to call the close method of the file object:

f.close()


When I run the above script, the field postPos doesn't match the input!
postPost should be the same as words[3]

You only set postPos once, outside the loop, and then never update it inside the loop. Follow the execution chain by hand:

lines = ["a,b,c,d,e", "f,g,h,i,j,k"]

Somehow postPos gets set to something before the loop starts. I don't know how, because you haven't shown us that part of the code. But let's say postPos is set to "z", just as an example. So you enter the for-loop:

line gets set to "a,b,c,d,e"
words get sets to ["a", "b", "c", "d", "e"]
print words[3], postPos
=> prints "d", "z"

line gets set to "f,g,h,i,j,k"
words get sets to ["f", "g", "h", "i", "j", "k"]
print words[3], postPos
=> prints "i", "z"



--
Steven

_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Reply via email to