Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using generators outside of a loop

Relatively new to python so please excuse the newbie question, but google isn't helpful at this time.

I have 100 very large xlsx files from which I need to extract the first row (specifically cell A2). I found this gem of a tool called openpyxl which will iterate through my data files without loading everything in memory. It uses a generaotor to get the relevant row on each call

The thing that I can't get is how to initialize a generator outside of a loop. Right now my code is:

from openpyxl import load_workbook
wb = load_workbook(filename = "merged01.xlsx", use_iterators= True)
sheetName = wb.get_sheet_names()
ws = wb.get_sheet_by_name(name = sheetName[0])
row = ws.iter_rows() #row is a generator
for cell in row:
    break
print (cell[1].internal_value) # A2

But there has to be a better way of doing this such as:

...
row = ws.iter_rows() #row is a generator
cell = row.first # line I'm trying to KISS
print (cell[1].internal_value) # A2
like image 739
Rasman Avatar asked Dec 09 '22 13:12

Rasman


1 Answers

cell = next(row)

The next function retrieves the next value from any iterator.

like image 55
user2357112 supports Monica Avatar answered Dec 11 '22 10:12

user2357112 supports Monica