[excel与dict] python 读取excel内容并放入字典、将字典内容写入 excel文件

一 读取excel内容、并放入字典

1 读取excel文件

python 复制代码
import pandas as pd

file_path = '/cluster/home3/zjc/Code/COD/BGNet_My/Dy_BGNet_master/openpyxl-light.xlsx' 读取excel 

raw_data = pd.read_excel(file_path, header=0)  # header=0表示第一行是表头,就自动去除了
print(raw_data)  # 读取到的结果如下
'''
读取excel
(Pdb) raw_data
       num                                            name       wfm
0        0            COD10K-CAM-1-Aquatic-1-BatFish-2.png  0.899108
1        1            COD10K-CAM-1-Aquatic-1-BatFish-4.png  0.885333
2        2            COD10K-CAM-1-Aquatic-1-BatFish-5.png  0.899334
3        3            COD10K-CAM-1-Aquatic-1-BatFish-6.png  0.503472
4        4  COD10K-CAM-1-Aquatic-10-LeafySeaDragon-416.png  0.422056
...    ...                                             ...       ...
2021  2021            COD10K-CAM-5-Other-69-Other-5051.png  0.973371
2022  2022            COD10K-CAM-5-Other-69-Other-5059.png  0.745310
2023  2023            COD10K-CAM-5-Other-69-Other-5060.png  0.000198
2024  2024            COD10K-CAM-5-Other-69-Other-5062.png  0.835672
2025  2025            COD10K-CAM-5-Other-69-Other-5063.png  0.982474
'''

2 读取value,舍弃行号

python 复制代码
data = raw_data.values # 获取其中内容(num、name、wfm)(舍弃行号)
print(data)
'''
仅展示value,舍弃行号
array([[0, 'COD10K-CAM-1-Aquatic-1-BatFish-2.png', 0.8991082232693329],
       [1, 'COD10K-CAM-1-Aquatic-1-BatFish-4.png', 0.8853328644290668],
       [2, 'COD10K-CAM-1-Aquatic-1-BatFish-5.png', 0.8993339821090026],
       ...,
       [2023, 'COD10K-CAM-5-Other-69-Other-5060.png',
        0.0001984435470317607],
       [2024, 'COD10K-CAM-5-Other-69-Other-5062.png', 0.8356721476832216],
       [2025, 'COD10K-CAM-5-Other-69-Other-5063.png', 0.9824740778028651]],
      dtype=object)
       
'''

3 读取为字典

python 复制代码
dict = {}
for i in range(len(data)):
    dict[data[i][1]] = data[i][2]

print(dict)
'''
将下面数据放入字典
data[0][1]  -> 'COD10K-CAM-1-Aquatic-1-BatFish-2.png'
data[0][2]  ->  0.8991082232693329

字典为
dict['COD10K-CAM-1-Aquatic-1-BatFish-2.png']
-> 0.8991082232693329
'''

一 读取excel内容、并放入字典(完整代码)

python 复制代码
import pandas as pd

file_path = '/cluster/home3/zjc/Code/COD/BGNet_My/Dy_BGNet_master/openpyxl-light.xlsx'
# r对路径进行转义,windows需要
raw_data = pd.read_excel(file_path, header=0)  # header=0表示第一行是表头,就自动去除了
print(raw_data)
'''
读取excel
(Pdb) raw_data
       num                                            name       wfm
0        0            COD10K-CAM-1-Aquatic-1-BatFish-2.png  0.899108
1        1            COD10K-CAM-1-Aquatic-1-BatFish-4.png  0.885333
2        2            COD10K-CAM-1-Aquatic-1-BatFish-5.png  0.899334
3        3            COD10K-CAM-1-Aquatic-1-BatFish-6.png  0.503472
4        4  COD10K-CAM-1-Aquatic-10-LeafySeaDragon-416.png  0.422056
...    ...                                             ...       ...
2021  2021            COD10K-CAM-5-Other-69-Other-5051.png  0.973371
2022  2022            COD10K-CAM-5-Other-69-Other-5059.png  0.745310
2023  2023            COD10K-CAM-5-Other-69-Other-5060.png  0.000198
2024  2024            COD10K-CAM-5-Other-69-Other-5062.png  0.835672
2025  2025            COD10K-CAM-5-Other-69-Other-5063.png  0.982474
'''

data = raw_data.values
print(data)
'''
仅展示value,舍弃行号
array([[0, 'COD10K-CAM-1-Aquatic-1-BatFish-2.png', 0.8991082232693329],
       [1, 'COD10K-CAM-1-Aquatic-1-BatFish-4.png', 0.8853328644290668],
       [2, 'COD10K-CAM-1-Aquatic-1-BatFish-5.png', 0.8993339821090026],
       ...,
       [2023, 'COD10K-CAM-5-Other-69-Other-5060.png',
        0.0001984435470317607],
       [2024, 'COD10K-CAM-5-Other-69-Other-5062.png', 0.8356721476832216],
       [2025, 'COD10K-CAM-5-Other-69-Other-5063.png', 0.9824740778028651]],
      dtype=object)
      
'''


dict = {}
for i in range(len(data)):
    dict[data[i][1]] = data[i][2]

print(dict)
'''
将下面数据放入字典
data[0][1]  -> 'COD10K-CAM-1-Aquatic-1-BatFish-2.png'
data[0][2]  ->  0.8991082232693329

字典为
dict['COD10K-CAM-1-Aquatic-1-BatFish-2.png']
-> 0.8991082232693329
'''
import pdb
pdb.set_trace()

二、将字典内容写入 excel文件

1

假设已有字典内容为:

即student列表里有4个字典,

第一个字典里面有3对key-value

"num": 1,

"name": "cod1",

"wfm": 0.1

python 复制代码
student = [
    {"num": 1, "name": "cod1", "wfm": 0.1},
    {"num": 2, "name": "cod2", "wfm": 0.2},
    {"num": 3, "name": "cod3", "wfm": 0.3},
    {"num": 4, "name": "cod4", "wfm": 0.4},
]

2 导入Workbook并实力化

python 复制代码
from openpyxl import Workbook
workbook = Workbook()

3 激活sheet-设置sheet名-插入标题-

python 复制代码
# 默认sheet
sheet = workbook.active  # 激活sheet  
sheet.title = "openpyxl"  # 设置sheet名字
sheet.append(["num", "name", "wfm"])  # 插入标题
for data in student:  # 列表循环读取dict
    sheet.append(list(data.values()))  
workbook.save("openpyxl.xlsx")

二、将字典内容写入 excel文件(完整代码)

python 复制代码
student = [
    {"num": 1, "name": "cod1", "wfm": 0.1},
    {"num": 2, "name": "cod2", "wfm": 0.2},
    {"num": 3, "name": "cod3", "wfm": 0.3},
    {"num": 4, "name": "cod4", "wfm": 0.4},
]
import pdb
pdb.set_trace()


from openpyxl import Workbook
workbook = Workbook()

# 默认sheet
sheet = workbook.active  # 激活sheet
sheet.title = "openpyxl"  # 设置sheet名字
sheet.append(["num", "name", "wfm"])  # 插入标题
for data in student:  # 列表循环读取dict
    sheet.append(list(data.values()))
workbook.save("openpyxl.xlsx")
相关推荐
爱吃生蚝的于勒41 分钟前
C语言内存函数
c语言·开发语言·数据结构·c++·学习·算法
小白学大数据3 小时前
Python爬虫开发中的分析与方案制定
开发语言·c++·爬虫·python
冰芒猓4 小时前
SpringMVC数据校验、数据格式化处理、国际化设置
开发语言·maven
Shy9604184 小时前
Doc2Vec句子向量
python·语言模型
失落的香蕉4 小时前
C语言串讲-2之指针和结构体
java·c语言·开发语言
红中马喽4 小时前
JS学习日记(webAPI—DOM)
开发语言·前端·javascript·笔记·vscode·学习
杜杜的man4 小时前
【go从零单排】Closing Channels通道关闭、Range over Channels
开发语言·后端·golang
java小吕布5 小时前
Java中Properties的使用详解
java·开发语言·后端
versatile_zpc5 小时前
C++初阶:类和对象(上)
开发语言·c++
尘浮生5 小时前
Java项目实战II基于微信小程序的移动学习平台的设计与实现(开发文档+数据库+源码)
java·开发语言·数据库·spring boot·学习·微信小程序·小程序