繁体   English   中英

我想以python中的字典的形式显示Excel的一列

[英]I want to display a column of Excel in the form of a dictionary in python

我的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)
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM