473,372 Members | 1,126 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,372 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 4275
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

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

Similar topics

15
by: John Machin | last post by:
I am pleased to announce a new general release (0.5.2) of xlrd, a Python package for extracting data from Microsoft Excel spreadsheets. CHANGES: * Book and sheet objects can now be pickled and...
11
by: Tempo | last post by:
Hello. I am getting the error that is displayed below, and I know exactly why it occurs. I posted some of my program's code below, and if you look at it you will see that the error terminates the...
2
by: BrendanC | last post by:
I've started learninhg Python and have developed a small Python app that imports Excel data into an Access mdb/jet database. This application has dependencies on the following: XLRD -...
2
by: kylancal | last post by:
I am trying to read an Excel book with XLRD and I am getting the following error Traceback (most recent call last): File "C:\temp\ReadGoldmanExcelFiles.py", line 62, in <module> startRow = 0,...
1
by: JYOUNG79 | last post by:
When running 'python setup.py install' to install items for xlrd to work, does anybody know what items are installed and where items are installed at on a Mac (OS 10.4)? I'm assuming it mainly...
1
by: Krishna | last post by:
I want to delete some rows (by creating a loop may be) using xlrd. Is this possible, if not how do I do that with python? Please help Thanks Krishna
3
by: Chanman | last post by:
This is probably a simple question to most of you, but here goes. I've downloaded the xlrd (version 0.6.1) module and placed in in the site-packages folder. Now, when I write a script, I type: ...
1
by: Edwin.Madari | last post by:
here is working code that will read & display contents of all rows & columns in all the sheets, you need xlrd 0.6.1 import xlrd, os, sys book = xlrd.open_workbook(sys.argv) print "The number...
2
by: patrick.waldo | last post by:
Hi all, I am trying to figure out a way to read colors with xlrd, but I did not understand the formatting.py module. Basically, I want to sort rows that are red or green. My initial attempt...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.