2

I'm trying to read some data from an excel file in python

I want to place the data into a 2D list

[[A,1,2...],[B,2,3...],[C,5,6...]]

where A, B, C are the col headers and the numbers are the rows in that column

right now I'm using just to test if this will work so i know the code doesn't do exactly what i said above

#Blank list array
product = []
k = 0
#Read in Data
for r in range(1,sheet.nrows):
    k += 1;
    product[k,1] = sheet.cell(r,1).value

im getting errors saying that list indices must be integers, not tuples is there a way to populate a list like this with a variable because I do not know how many rows a column might have in the excel file I've heard of dictionaries but from what I've read I don't think those would be the best option for me since they are more "key" "value" and i have multiple values for one "key"

karthikr
  • 97,368
  • 26
  • 197
  • 188
shreddish
  • 1,637
  • 8
  • 24
  • 38
  • 2
    Change `product[k,1]` to `product[k][1]` – karthikr Jun 12 '13 at 14:12
  • 1
    You could accomplish this with a dictionary, see: http://stackoverflow.com/questions/5378231/python-list-to-dictionary-multiple-values-per-key – Andreas GS Jun 12 '13 at 14:16
  • @Kathikr now I'm getting list index out of range – shreddish Jun 12 '13 at 14:23
  • Andrea doesn't he/she already have his list populated in that one tho? I need to basically populate my list and I don't know how long it needs to be – shreddish Jun 12 '13 at 14:24
  • What are you trying to do there – karthikr Jun 12 '13 at 14:31
  • @karthikr i changed product[k,1] to product[k][1] and i get that the list index is out of range – shreddish Jun 12 '13 at 15:02
  • @Reddman - lost reception before I could finish. If you are having trouble reading in rows use the CSV module. Parse each row such that that values you get are appended to the proper lists- [A,..],[B,..] etc. You also want a list of lists it looks like. See this article [link](http://mail.python.org/pipermail/tutor/2000-January/000943.html) – Andreas GS Jun 12 '13 at 15:44
  • @AndreaGS thank you for your help however I do not want to read CSV files I'm trying to read straight from an excel file – shreddish Jun 12 '13 at 16:10

1 Answers1

2

A dictionary would work for you, as each value can be a list.

product = {}
for col in range(sheet.ncols):
    values = []
    for row in range(1, sheet.nrows):
        values.append(sheet.cell(row, col).value)
    product[sheet.cell(0, col)] = values

This would then allow you to access any column's list of values by the column label.

> print product['B']
> [1, 4, 2, 6, 7]

EDIT: Edited to change from parsing rows to columns instead.

Michael Davis
  • 2,350
  • 2
  • 21
  • 29
  • seems like it will work with a few tweaks your parsing is backwards as to what I want to do... I want to parse every row in column1 then every row in column2, you are parsing every column in row1 and so on... I tried with following code for col in range(sheet.ncols): values = [] for row in range(sheet.nrows): values.append(sheet.cell(row, col).value) product[col] = values print product[1] and i get funky result when i print product[1] i get: [u'B',3.0,4.0] I think the value.append only works with ints so its telling me u'B' its a string? – shreddish Jun 12 '13 at 15:04
  • im sorry for the horrible formatting i cant get it right in this comment box – shreddish Jun 12 '13 at 15:05
  • 1
    No, that is working fine. The 'u' at the beginning of the string is just to signify that it is a unicode string. I will add an edit to swap how things are parsed. – Michael Davis Jun 12 '13 at 17:47
  • My current snippet assumes that you don't want the label value as one of the values in the column info. – Michael Davis Jun 12 '13 at 17:53