1

I would like to loop through an excel table and get the values for selected columns in each row in a set of lists or dictionary. if in a dictionary, for each row, the value in the first selected column would be the key and the values in the other selected columns would be the values (array) for that key. I cannot figure out how to tell python to read values from only selected columns...for the excel table may have 10 columns but I am only interest in three for example, and the three of interest are not contiguous. Would appreciate your insights using XLRD.

    import xlrd
    from xlrd import open_workbook
    import arcpy

    wb = open_workbook ("c:\\Users\\Admin\\Documents\\Marion\\Courses\\GEOG485\\FinalProject\\Data\\ExcelFiles\\Belize_Culvert_Nov15_V4.0.xlsx")

    sheet = wb.sheet_by_index(1)

    keys = [sheet.cell(0, 5).value for col_index in xrange(sheet.ncols)]

    dict_list = []
    for rownum in range(sheet.nrows):
        d = {keys[col_index]: sheet.cell(0, 5).value 
        for col_index in xrange(sheet.ncols)}:
            dict_list.append(d)

The field that I want to use as key is column 5 and the values are columns #16 and #17 as an array value for each key...

2
  • import xlrd from xlrd import open_workbook import arcpy wb = open_workbook ("c:\\Users\\Admin\\Documents\\Marion\\Courses\\GEOG485\\FinalProject\\Data\\ExcelFiles\\Belize_Culvert_Nov15_V4.0.xlsx") print wb.nsheets sheet = wb.sheet_by_index(1) keys = [sheet.cell(0, 5).value for col_index in xrange(sheet.ncols)] dict_list = [] for row_index in xrange(1, sheet.nrows): d = {keys[col_index]: sheet.cell(row_index, col_index).value for col_index in xrange(sheet.ncols)} dict_list.append(d) print dict_list Commented Jul 27, 2015 at 15:08
  • Please edit your question to include this code, and state what worked, and what didn't. Commented Jul 27, 2015 at 15:45

2 Answers 2

3

Use this command

row_slice(rowx, start_colx=0, end_colx=None)

Returns a slice of the Cell objects in the given row.

Sign up to request clarification or add additional context in comments.

Comments

1

Some issues in your code -

  1. keys = [sheet.cell(0, 5).value for col_index in xrange(sheet.ncols)] - This always takes the keys as the value in the first row and 6th column (Rows and columns are 0 indexed.)

  2. d = {keys[col_index]: sheet.cell(0, 5).value - This is not even valid python syntax

You can just loop over all the rows, take column index 4 (5th column) as key and the rest in a list and add that to a dictionary, Example -

import xlrd
from xlrd import open_workbook
import arcpy

wb = open_workbook ("c:\\Users\\Admin\\Documents\\Marion\\Courses\\GEOG485\\FinalProject\\Data\\ExcelFiles\\Belize_Culvert_Nov15_V4.0.xlsx")

sheet = wb.sheet_by_index(1)
sheetdict = {}
for rownum in range(sheet.nrows):
    sheetdict[sheet.cell(rownum,4)] = [sheet.cell(rownum,15),sheet.cell(rownum,16)]

In the end, sheetdict has the required dictionary.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.