471,354 Members | 1,753 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,354 software developers and data experts.

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:
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
3 4141
dazzler
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
2,851 Expert Mod 2GB
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
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.

Similar topics

2 posts views Thread by kylancal | last post: by
1 post views Thread by JYOUNG79 | last post: by
1 post views Thread by Krishna | last post: by
3 posts views Thread by Chanman | last post: by
1 post views Thread by Edwin.Madari | last post: by
2 posts views Thread by patrick.waldo | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.