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

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

1 读取excel文件

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,舍弃行号

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 读取为字典


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内容、并放入字典(完整代码)

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

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并实力化


from openpyxl import Workbook
workbook = Workbook()

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

# 默认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文件(完整代码)



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")

你可能感兴趣的:(python,excel,开发语言)