Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #49401

Re: indexerror: list index out of range??

Newsgroups comp.lang.python
Date 2013-06-28 20:35 -0700
References <8b12635e-c3e9-49b2-b3fa-792559088821@googlegroups.com>
Message-ID <c6642bec-58b1-4db2-bad9-6f85cb3cf5c6@googlegroups.com> (permalink)
Subject Re: indexerror: list index out of range??
From Titiksha <joshi.titiksha90@gmail.com>

Show all headers | View raw


On Friday, June 28, 2013 8:20:28 PM UTC-5, Titiksha wrote:
> Hi,
> 
> I am working on the following code but am getting the error: list index out of range. I surfed through the group but somehow I am not able to fix my error.Please guide.Structure is given below:
> 
> m is a list of 5 elements. I have to match elements of m from fields in file ALL_BUSES_FINAL.cvs.
> 
> m=['631138', '601034', '2834', '2908', '64808']
> 
> 
> 
> i=0
> 
> while i<len(m):
> 
>     print(i)
> 
>     my_file = open("ALL_BUSES_FINAL.csv", "r+")
> 
>     for line in my_file:
> 
>         if m[i] in line:
> 
>             print (line)
> 
>             a.append(line)
> 
>             i+=1
> 
>             print(a)
> 
>     my_file.close()
> 
> 
> 
> 
> 
> The output is as follows:
> 
> 0
> 
> LAKEFLD  3227,631138
> 
> 
> 
> ['LAKEFLD  3227,631138\n']
> 
> 
> 
> 1
> 
> NOBLES   3013,601034
> 
> 
> 
> ['LAKEFLD  3227,631138\n', 'NOBLES   3013,601034\n']
> 
> 
> 
> 2
> 
> GR_ISLD  I,2834
> 
> 
> 
> ['LAKEFLD  3227,631138\n', 'NOBLES   3013,601034\n', 'GR_ISLD  I,2834\n']
> 
> 
> 
> FTTHOMP  928,2908
> 
> 
> 
> ['LAKEFLD  3227,631138\n', 'NOBLES   3013,601034\n', 'GR_ISLD  I,2834\n', 'FTTHOMP  928,2908\n']
> 
> 
> 
> VICTRYH  15,64808
> 
> 
> 
> ['LAKEFLD  3227,631138\n', 'NOBLES   3013,601034\n', 'GR_ISLD  I,2834\n', 'FTTHOMP  928,2908\n', 'VICTRYH  15,64808\n']
> 
> Traceback (most recent call last):
> 
>   File "C:\Users\TJ\dist_tracking.py", line 40, in <module>
> 
>     if m[i] in line:
> 
> IndexError: list index out of range
> 
> >>> 
> 
> 
> 
> I see the line,a being correct but print (i) does not show up after 2. and index error comes up. I am too confused now. Please guide.
> 
> Thanks in advance.

Thanks for helping out! 
Dave you mentioned about false matches in case of string in m is substring of line. How do I manage that issue? Is there any other method I should look into?

What I am looking to do is..I have a list of m which I need to map in the same sequence to the ALL_BUSES_FINAL file and get the entire line which has the string in m.I want to iterate through all the lines in ALL_BUSES_FINAL to match the all strings in m.



Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

indexerror: list index out of range?? Titiksha Joshi <joshi.titiksha90@gmail.com> - 2013-06-28 18:20 -0700
  Re: indexerror: list index out of range?? Chris Angelico <rosuav@gmail.com> - 2013-06-29 11:53 +1000
  Re: indexerror: list index out of range?? Dave Angel <davea@davea.name> - 2013-06-28 21:58 -0400
  Re: indexerror: list index out of range?? Titiksha <joshi.titiksha90@gmail.com> - 2013-06-28 20:35 -0700
    Re: indexerror: list index out of range?? Dave Angel <davea@davea.name> - 2013-06-29 09:44 -0400
    Re: indexerror: list index out of range?? Mark Lawrence <breamoreboy@yahoo.co.uk> - 2013-06-29 15:30 +0100
    Re: indexerror: list index out of range?? Joshua Landau <joshua.landau.ws@gmail.com> - 2013-06-29 16:52 +0100
    Re: indexerror: list index out of range?? Robert Kern <robert.kern@gmail.com> - 2013-07-01 09:45 +0100
      Re: indexerror: list index out of range?? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-07-01 12:30 +0000

csiph-web