-1

Here is a question for python programming.

I am trying to build a dict by importing data from excel for analyzing.

my structure of the dictionary is:

dict = { sheet_name : 
                  {label_name1 : [['datatype'],['','',some_data.....]],      
                   label_name2 : [['datatype'],['','',some_data.....]],
                   ...
                  }
         sheet_name1 :
                  {label_name3 : [['datatype'],['','',some_data.....]],      
                   label_name4 : [['datatype'],['','',some_data.....]],
                   ...
                  }
       }

now i use the xlrd to process reading data.

file = xlrd.open_workbook(filename)

table_names = file.sheet_names()    

label = sum([file.sheet_by_name(names).row_values(0) for names in
         table_names], [])

SHEET_DICT = {names: {labels: [['unknown_datatype'], 
                               file.sheet_by_name(names)\
                                   .col_values(i for i \
                                   in file.sheet_by_name(names).ncols))]
                       for labels in label}               
              for names in table_names }

i got an error message:

Traceback (most recent call last): File "C:/Users/panze/PycharmProjects/Harvest/main.py", line 92, in test = read_excel(EXCELFILE) File "C:/Users/panze/PycharmProjects/Harvest/main.py", line 73, in read_excel for names in table_names } File "C:/Users/panze/PycharmProjects/Harvest/main.py", line 73, in for names in table_names } File "C:/Users/panze/PycharmProjects/Harvest/main.py", line 72, in for label in labels}

TypeError: 'int' object is not iterable

> file.sheet_by_name(names).col_values(0) is right 

> file.sheet_by_name(names).col_values(1) is right

when i try to use variable instead

file.sheet_by_name(names).col_values(i for i in range(29)) 

it is wrong.

however i need it makes iterable so that i can build a right dict.

is there other way to do that?

i really what to use dict comprehension to do this way ...

thanks for your help

El'tar
  • 1
  • 1
  • Please include the _complete_ error message. – DYZ Mar 27 '18 at 05:29
  • 1
    At a certain point you give up on comprehensions. They ultimately begin to hurt readability and reasoning once they reach a certain size. Unless its just for interest then just do it in a loop or define a function you call in the comprehension. – Paul Rooney Mar 27 '18 at 05:33
  • I'm not sure what you wanted `col_values(i for i in range(29))` to return. A list of 29 columns, each of which is a list of that column's value in every row? Or something different? – abarnert Mar 27 '18 at 05:35
  • yes, it is each value in every col – El'tar Mar 27 '18 at 05:40
  • 1
    What is the return value of the method `ncols`? If it is just an integer, the interpeter sees something like `for i in 29`, which is wrong code, because `29` is not an iterable object... – Sven-Eric Krüger Mar 27 '18 at 05:41
  • what is your expected output ? – Aaditya Ura Mar 27 '18 at 05:42

1 Answers1

1

According to the docs:

col_values(colx, start_rowx=0, end_rowx=None)

Returns a slice of the values of the cells in the given column.

So, you don't pass it an iterable like i for i in range(29) (which, by the way, is just a more verbose and slower way to get the same values as just range(29)), you pass it a column number, start row, and end row.

If you want to get a list of column 0, column 1, column 2, etc., you can do that like this:

[file.sheet_by_name(names).col_values(i) for i in range(29)]

If you're trying to get a list of column 0, column 1, etc. just for a specific row, you can instead use the row_values function, passing the row number and then the start and end column.

abarnert
  • 354,177
  • 51
  • 601
  • 671