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: -
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 -
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: -
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
3 4277
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
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: - >>> countries = ['A', 'A', 'B', 'C', 'C', 'D', 'D', 'D', 'E']
-
>>> unique_countries = []
-
>>> for country in countries:
-
... if country not in unique_countries:
-
... unique_countries.append(country)
-
...
-
>>> unique_countries
-
['A', 'B', 'C', 'D', 'E']
-
>>>
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: - >>> countries = ['A', 'A', 'B', 'C', 'C', 'D', 'D', 'D', 'E']
-
>>> unique_countries = []
-
>>> for country in countries:
-
... if country not in unique_countries:
-
... unique_countries.append(country)
-
...
-
>>> unique_countries
-
['A', 'B', 'C', 'D', 'E']
-
>>>
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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 -...
|
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,...
|
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...
|
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
|
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:
...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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: 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...
| |