Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 如何在python中从excel电子表格中将数据提取到DICT列表中?_Python 3.x_Xlrd - Fatal编程技术网

Python 3.x 如何在python中从excel电子表格中将数据提取到DICT列表中?

Python 3.x 如何在python中从excel电子表格中将数据提取到DICT列表中?,python-3.x,xlrd,Python 3.x,Xlrd,我正在尝试使用xlrd python库解析excel电子表格 上图是我的电子表格中的样本数据。我正试图像这样解析这些数据 'genders':[{'gender':'male','country':'USA'},{'gender':'female','country':'canada'}] 但我还是做不到 我尝试了data=[[sheet.cell\u value(r,c)表示范围内的c(sheet.ncols)]表示范围内的r(sheet.nrows)],但我没有看到我要查找的数据 有人能帮

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

上图是我的电子表格中的样本数据。我正试图像这样解析这些数据
'genders':[{'gender':'male','country':'USA'},{'gender':'female','country':'canada'}]
但我还是做不到

我尝试了
data=[[sheet.cell\u value(r,c)表示范围内的c(sheet.ncols)]表示范围内的r(sheet.nrows)]
,但我没有看到我要查找的数据


有人能帮忙吗。

您的代码很适合读取xls文件。 但是您希望
序列化数据。您指定了必须如何输出,这是数据的过程,所以。。。这里有一个快速而基本的解决方案

#使用Python读取excel文件
导入xlrd
#给出文件的位置。
loc=(“file.xls”)
#打开工作簿
wb=xlrd.open_工作簿(loc)
#对工作表无任何评论,请说明您只有一张工作表,设置为0(第一张)
工作表=工作表。工作表按索引(0)
#输出是序列化对象
输出={'genders':[]}
#我们将对行进行迭代,首先跳过头(0+1)
对于范围内的i(0+1,表N):
#由于第一列为空,我们将使用_
_,性别,国家=表。行_值(i)
#填充序列化对象
输出['genders'].append({'gender':性别,
“国家”:国家})
打印(输出)
我的输出:

/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

非常感谢@gilito.to您@实际上,我们如何知道表中将有多少行。假设一张桌子下可以有20行,也可以没有。有办法检测到吗?是的,sheet.nrows会告诉你的。参见文档->