簡體   English   中英

在 Python 中使用 xlrd 讀取.xls

[英]Read .xls with xlrd in Python

我正在嘗試獲取 an.xls 文檔中每個鏡頭的背景列表,但我不知道如何說在不同鏡頭時停止讀取列和行...

我正在閱讀的.xls 是這樣的:

在此處輸入圖像描述

我的測試代碼在那里:

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    

我的結果日志包含 .xls 中的所有背景,但我只需要鏡頭“002”的背景(這里只有 3 個背景)。 有人知道如何只為一個鏡頭閱讀背景嗎?

最好的選擇是使用Panda Dataframes 清理數據和使用它非常容易。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM