1

I have an excel sheet which looks like

Name           email                desc           date
name1          email1               desc1          date1
name2          email2               desc2          date2
name3          email3               desc3          date3

Im looping through and inserting the data into an array and printing information on each row

import xlrd
from openpyxl import load_workbook
book = xlrd.open_workbook('example.xlsx')
sheet = book.sheet_by_name('Sheet1')
row_count = sheet.nrows
curr_row = 0
row_array = []
while curr_row < row_count:
  row = sheet.row(curr_row)
  row_array += row
  curr_row +=1
  print row_array

This adds everything into one array.

I want to be able to call

row_array[1][2] and desc1 comes up or 
row_array[3][0] and name 3 comes up. 

How can i achieve this

1 Answer 1

2

Try that:

>>> from openpyxl import load_workbook
>>> wb = load_workbook('example.xlsx', read_only=True)
>>> ws = wb.active
>>> data = [[cell.value for cell in row] for row in ws.rows]
>>> data[1][2]
'desc1'
>>> data[3][0]
'name3'

The data list may be a little obscure, unrolling the code would give:

data = []
for i, row in enumerate(ws.rows):
    data.append([])
    for cell in row:
        data[i].append(cell.value)
Sign up to request clarification or add additional context in comments.

1 Comment

Little reminder: stackoverflow.com/help/someone-answers, if the answer is good for you :)

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.