File traversing
Nibin V M
nibinvm at gmail.com
Sun Apr 15 05:15:29 EDT 2012
Hello,
First of all..I am very new to python with no background in development
area! :)
Ok, here is my problem.....I have opened a file and I need to check each
line of that file. I have done it with a while loop.
res_own_file = open('/bah')
res_own_list = res_own_file.readline()
res_tot_list=[]
while res_own_list:
res_own_list=res_own_list.strip()
res_own_list=res_own_list.replace(' ', '')
res_name=res_own_list.split(':')
if res_name[1:2] not in res_tot_list:
res_tot_list.append(res_name[1:2])
res_own_list = res_own_file.readline()
As you can see above, I am reading each line, cut a particular field and
sort a unique list from it...I have two questions here
1. If I have to check the file again from beginning, I can't do it without
closing and re-opening file since the file pointer is assigned to the EOF
of the opened file right? If so, is there any alliterative method to do it?
I don't wish to mess the script with multiple file open-close code!
2. If I print res_name, it will display like ['one', 'two']. If I
print res_name[0], it will display one ; but if I print res_name[1] it
will display error out of index instead of two. From my code, when I use
res_name[1:2] it's displaying second filed. Why it is behaving like this?
Thanks in advance!
Regards....
Nibin.
http://TechsWare.in
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20120415/cb06ec59/attachment.html>
More information about the Python-list
mailing list