如何从 python 中的 excel 电子表格中将数据提取到字典列表中?

how to extract data into list of dicts from excel spreadsheet in python?

我正在尝试使用 xlrd python 库解析 excel 电子表格。

上图是我的电子表格中的示例数据。我正在尝试像这样解析这些数据 'genders': [{'gender': 'male', 'country': 'USA'}, {'gender': 'female', 'country': 'canada'}] 但不太明白。

我已经尝试 data = [[sheet.cell_value(r, c) for c in range(sheet.ncols)] for r in range(sheet.nrows)] 但我没有看到我正在寻找的数据。

有人可以帮忙吗

您的代码适合读取 xls 文件。 但是您想 serialize 您的数据。您指定了必须如何输出,这是 serialize 数据的过程,所以...这是一个快速和基本的解决方案。

# Reading an excel file using Python
import xlrd

# Give the location of the file.
loc = ("file.xls")

# To open Workbook
wb = xlrd.open_workbook(loc)

# no comment about sheets, understant that you have only one sheet, set 0 ( first) 
sheet = wb.sheet_by_index(0)

# output is the serialized object
output = {'genders': []}

# we will iterate over rows, skipping first that is header ( 0+1 )
for i in range(0+1, sheet.nrows):
    # As first column is empty, we will assing to _
    _, gender, country = sheet.row_values(i)
    
    # filling serialized object
    output['genders'].append({'gender': gender,
                              'country': country})

print(output)

我的输出:

/home/gil/PycharmProjects/testing/venv/bin/python /home/gil/PycharmProjects/testing/soxlrd.py
{'genders': [{'gender': 'caca', 'country': 'bebe'}, {'gender': 'cece', 'country': 'dede'}, {'gender': 'ee', 'country': 'ff'}, {'gender': 'gg', 'country': 'hh'}]}

Process finished with exit code 0