XLRD: Parsing info from a column

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • waldopat
    New Member
    • Nov 2007
    • 2

    XLRD: Parsing info from a column

    Hi all,

    I have an excel doc that has some information about various countries and I want to get a list of all the countries, which is in Column A. But, when I try to iterate over column A, I get this error:
    Code:
    Traceback (most recent call last):
      File "C:\Python24\Lib\site-packages\pythonwin\pywin\framework\scriptutils.py", line 310, in RunScript
        exec codeObject in __main__.__dict__
      File "C:\Documents and Settings\PW\My Documents\Python\excel\read_matrix_clean.py", line 18, in ?
        if countries[n]==countries[n+1]:
    IndexError: list index out of range
    I don't quite understand, because if I try
    Code:
    print countries[n]
    print countries[n+1]
    print countries[n+2]
    etc...it prints all the correct information.

    Thanks for any help!
    Patrick

    Here's the code:
    Code:
    import xlrd
    
    path_file = "c:\\data.xls"
    book = xlrd.open_workbook(path_file)
    Counts = book.sheet_by_index(1)
    countries= Counts.col_values(0,start_rowx=1, end_rowx=None)
    
    n = 0
    x = len(countries)
    print x
    
    while n<x:
        if countries[n]==countries[n+1]:
            del countries[n+1]
        else:
            n = n+1
    print countries
    Last edited by bartonc; Nov 15 '07, 05:20 PM. Reason: Added [CODE][/CODE] tags.
  • dazzler
    New Member
    • Nov 2007
    • 75

    #2
    Originally posted by waldopat

    x = len(countries)
    while n<x:
    if countries[n]==countries[n+1]:
    n +=1
    if for example list lenght is 10 , last item in list is list[9] (and first is list[0])

    if your countries lenght is 100, and last time when you are in a loop it tries "if counries[99] == counties[100]: (and there's error because last item in your list is countries[99] not [100] )

    my brain's not working so well right now (been coding 10hours now), but hopefully that was the error in your code ;D

    Comment

    • bvdet
      Recognized Expert Specialist
      • Oct 2006
      • 2851

      #3
      Originally posted by waldopat
      while n<x:
      if countries[n]==countries[n+1]:
      del countries[n+1]
      else:
      n = n+1
      print countries
      Your problem is in your loop. When you delete an item from your list, the length of the list changes but the variable 'x' does not. Try something like this:[code=Python]>>> countries = ['A', 'A', 'B', 'C', 'C', 'D', 'D', 'D', 'E']
      >>> unique_countrie s = []
      >>> for country in countries:
      ... if country not in unique_countrie s:
      ... unique_countrie s.append(countr y)
      ...
      >>> unique_countrie s
      ['A', 'B', 'C', 'D', 'E']
      >>> [/code]

      Comment

      • waldopat
        New Member
        • Nov 2007
        • 2

        #4
        Great! That was it. For some reason I was thinking too hard about it. if not in was exactly what I should have done!

        Thanks a lot

        Originally posted by bvdet
        Your problem is in your loop. When you delete an item from your list, the length of the list changes but the variable 'x' does not. Try something like this:[code=Python]>>> countries = ['A', 'A', 'B', 'C', 'C', 'D', 'D', 'D', 'E']
        >>> unique_countrie s = []
        >>> for country in countries:
        ... if country not in unique_countrie s:
        ... unique_countrie s.append(countr y)
        ...
        >>> unique_countrie s
        ['A', 'B', 'C', 'D', 'E']
        >>> [/code]
        Last edited by bartonc; Nov 15 '07, 11:30 PM.

        Comment

        Working...