[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")
相关推荐
clock的时钟11 分钟前
c++第七天--继承与派生
开发语言·c++
cylat12 分钟前
Day23 pipeline管道
人工智能·python·算法·机器学习
蓝桉~MLGT41 分钟前
java高级——高阶函数、如何定义一个函数式接口类似stream流的filter
java·开发语言·python
IOT.FIVE.NO.11 小时前
Conda安装pytorch和cuda出现问题的解决记录
人工智能·pytorch·python
点云SLAM1 小时前
C++中string流知识详解和示例
开发语言·c++·istringstream·ostringstream·c++学习·stringstream·数据流操作
山海不说话7 小时前
视频行为标注工具BehaviLabel(源码+使用介绍+Windows.Exe版本)
人工智能·python·计算机视觉·视觉检测
门前云梦9 小时前
《C语言·源初法典》---C语言基础(上)
c语言·开发语言·学习
liuzhenghua669 小时前
Python任务调度模型
java·运维·python
小前端大牛马9 小时前
java教程笔记(十一)-泛型
java·笔记·python
sjtu_cjs9 小时前
Tensorrt python api 10.11.0笔记
开发语言·笔记·python