简体   繁体   中英

Read .xls with xlrd in Python

I'm trying to get a list of backgrounds for each shots in an.xls document but I have no idea how to say to stop reading column and rows when it's a different shot...

The.xls I'm reading is like this:

在此处输入图像描述

and my test code is there:

import xlrd

planNameToFind = '002'
backgroundsList = []

openFolderPath = 'I:\\manue\\REFS\\516\\ALCM_516_SceneAssetList.xls'.format().replace('/','\\')
wb = xlrd.open_workbook(openFolderPath)
sheets = wb.sheet_by_index(0)
nrows = sheets.nrows
allcols = sheets.ncols
episode_index = 0
shot_index = 2
background_index = 9
rst = 1
seqrow = rst + 1
for rowx in xrange(rst + 2, nrows + 1):
    planName = str(sheets.cell_value(seqrow, shot_index)).replace('.0', '')
    if planName == planNameToFind:
        print 'planName',planName
        background = str(sheets.cell_value(seqrow, background_index)).replace('.0', '')
        print 'background: ',background
        backgroundsList.append(background)
    if planName == '':
        background = str(sheets.cell_value(seqrow, background_index)).replace('.0', '')
        if background != '':
            print 'background2: ',background  
            backgroundsList.append(background)

    #shotToDo = ' shotToDo: {0} BG: {1}'.format(planName,background)

    seqrow += 1 
print 'backgroundsList: ',backgroundsList    

My result log alls the backgrounds in the.xls, but I need only backgrounds of shot '002' (here only 3 backgrounds). Does someone know how to read backgrounds only for a shot?

Your best bet is to work with Panda Dataframes . It's extremely easy to clean data and work with it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM