By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,097 Members | 1,529 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,097 IT Pros & Developers. It's quick & easy.

XLRD: Parsing info from a column

P: 2
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:
Expand|Select|Wrap|Line Numbers
  1. Traceback (most recent call last):
  2.   File "C:\Python24\Lib\site-packages\pythonwin\pywin\framework\scriptutils.py", line 310, in RunScript
  3.     exec codeObject in __main__.__dict__
  4.   File "C:\Documents and Settings\PW\My Documents\Python\excel\read_matrix_clean.py", line 18, in ?
  5.     if countries[n]==countries[n+1]:
  6. IndexError: list index out of range
  7.  
I don't quite understand, because if I try
Expand|Select|Wrap|Line Numbers
  1. print countries[n]
  2. print countries[n+1]
  3. print countries[n+2]
etc...it prints all the correct information.

Thanks for any help!
Patrick

Here's the code:
Expand|Select|Wrap|Line Numbers
  1. import xlrd
  2.  
  3. path_file = "c:\\data.xls"
  4. book = xlrd.open_workbook(path_file)
  5. Counts = book.sheet_by_index(1)
  6. countries= Counts.col_values(0,start_rowx=1, end_rowx=None)
  7.  
  8. n = 0
  9. x = len(countries)
  10. print x
  11.  
  12. while n<x:
  13.     if countries[n]==countries[n+1]:
  14.         del countries[n+1]
  15.     else:
  16.         n = n+1
  17. print countries
Nov 15 '07 #1
Share this Question
Share on Google+
3 Replies


P: 75

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
Nov 15 '07 #2

bvdet
Expert Mod 2.5K+
P: 2,851
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:
Expand|Select|Wrap|Line Numbers
  1. >>> countries = ['A', 'A', 'B', 'C', 'C', 'D', 'D', 'D', 'E']
  2. >>> unique_countries = []
  3. >>> for country in countries:
  4. ...     if country not in unique_countries:
  5. ...         unique_countries.append(country)
  6. ...         
  7. >>> unique_countries
  8. ['A', 'B', 'C', 'D', 'E']
  9. >>> 
Nov 15 '07 #3

P: 2
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

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:
Expand|Select|Wrap|Line Numbers
  1. >>> countries = ['A', 'A', 'B', 'C', 'C', 'D', 'D', 'D', 'E']
  2. >>> unique_countries = []
  3. >>> for country in countries:
  4. ...     if country not in unique_countries:
  5. ...         unique_countries.append(country)
  6. ...         
  7. >>> unique_countries
  8. ['A', 'B', 'C', 'D', 'E']
  9. >>> 
Nov 15 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.