473,382 Members | 1,368 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,382 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 4277
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: 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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.