python - 我想在python中以字典的形式显示一列Excel
问题描述
我的 excel 文件中有一列,我必须将该列的所有行转换为字典格式。我的文件像这样,有 802 行: 在此处输入图像描述
和我的预期输出:
ldict = {
"item1": set(["5"]),
"item2": set(["2"]),
"item3": set(["0"]),
"item4": set(["1"]),
"item5": set(["6"]),
"item6": set(["6"]),
"item7": set(["1"]),
} 请帮我。
解决方案
安装openpyxl
包(如果你还没有的话),然后执行:
import openpyxl
# Access active worksheet of excel file
book = openpyxl.load_workbook('workbook.xlsx')
sheet = book.active
# Access first column
column = sheet['A']
# Use dictionary comprehension on the cells in the column
d = {
'item{}'.format(num): cell.value
for (num, cell) in enumerate(column, 1)
}
推荐阅读
- linux - 如何在不输入密码的情况下签署 rpm 包?
- wordpress - 创建一个使用简码发送电子邮件的按钮(wordpress - 没有插件)
- dns - Google 如何在其 .app TLD 上强制使用 HTTPS?
- android - 删除android模型类Realm中的主键
- c# - 什么会导致 BackgroundWorker 在不抛出异常的情况下失败?
- stm32 - SD卡文件名作为日期戳
- clojure - Cider nREPL 使用 virtualbox 错误:远程对等点断开连接
- php - PHP conditional regex
- terraform - 如何实现 terraform 嵌套循环构造?
- sympy - 为什么 SymPy 不取代指数?