python - Pyexcel 保存到字典吗?

标签 python python-3.x

我正在尝试导入一个简单的 xls 文件并将数据添加到字典中。我可以将它们添加到列表中。问题出在字典上。

import pyexcel
import pyexcel.ext.xls
book = pyexcel.get_book(file_name="Test.xls")# The external file
names =  {}

names[book["Test"].column[5]] = [book["Sheet1"].column[0]]#Adding the data to the dictionary

下面是错误代码:

names[book["Test"].column[5]] = [book["Sheet1"].column[0]]
TypeError: unhashable type: 'list'

最佳答案

我不使用 Pyexcel,所以我的答案是基于从 2 个列表创建字典。如果 .column[5] 是键列表,.column[0] 是值列表,则创建一个名为名称的字典:

names = dict(zip(book["Test"].column[5], book["Sheet1"].column[0]))

以下是一个简单示例所表达的原理:

a = ['a', 'b', 'c']

b = [1,2,3]

dict(zip(a, b))
Out[3]: {'a': 1, 'b': 2, 'c': 3}

关于python - Pyexcel 保存到字典吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34725248/

相关文章:

Python 并发 : Wait for one out of few futures and cancel the rest

python - 在 vscode 中使用注释为大纲制作段落

python - 使用 vtkTubeFilter 创建闭环

python - 如何更新tensorflow中的 'eagertensor"对象

Python只运行一次while循环

python - 在 PyQt 中,在主窗口和线程之间共享数据的最佳方式是什么

python - 从 for 循环中提取特定的迭代输出

python - 区分同名节点的正确图形数据结构是什么?

python - 添加包含 `cairo-gobject.pc' 的目录

python : check if the nested dictionary exist