与列标题进行比较,并在python中检索其数据

问题描述 投票:0回答:1

我想做的是拿一个excel文件并用类别vise提取列。我所做的是这个我从excel中提取数据列表作为list1并仅提取标题作为标题,我采取一个基本列表和与标题的交集。结果我得到一个类别列表

list1如下:

[{'Title': 'Asam', 'Description': 'all about', 'Latitude': 47545.0, 'Longitude': 65564.0}]

标题如下:

['Title', 'Description', 'Latitude', 'Longitude']

基本列表如下:

{'Title','Description'}

我想输出就像:

[{'Title': 'Asam', 'Description': 'all about'}]

我得到的输出是:

['Title', 'Description']

所以,我试过这个:

def main():
    sheet = open_workbook(filename)
    sheet_names = sheet.sheet_names()
    for s in sheet_names:
        xl_sheet = None
        xl_sheet = sheet.sheet_by_name(s)

        header = [xl_sheet.cell(0, col_index).value for col_index in range(xl_sheet.ncols)]
        print(header)

        list_1 = []
        for row_index in range(1, xl_sheet.nrows):
            d = {header[col_index]: xl_sheet.cell(row_index, col_index).value
                 for col_index in range(xl_sheet.ncols)}
            list_1.append(d)

        print(list_1)

        basic = {'Title','Description', 'Location', 'Info'}
        lst3 = [value for value in header if value in basic]
        print(lst3)
python xlrd
1个回答
1
投票

您应该像这样创建list_3:

idx = 0
list_3 = [{value: list_1[idx][value] for value in header if value in basic}]
© www.soinside.com 2019 - 2024. All rights reserved.