如何将阵列转换为python的Excel?

发布于 2025-01-22 21:12:31 字数 3949 浏览 4 评论 0原文

我正在尝试将数据提取到Excel中,但我有问题 在此部分:

sheet.cell(row=i+2, column=7).value = OeFC1b.value

因此我使用print()并检查值。它是50.20042375032

我收到此错误消息:

raise ValueError("Cannot convert {0!r} to Excel".format(value))  
ValueError: Cannot convert array(50.20042375) to Excel

为什么是一个数组?如何提取数据?

这是整个代码:

# -*- coding: utf-8 -*-
"""
Created on Sat Apr 16 18:18:01 2022

@author: mskim
"""

import pandas as pd
import cvxpy as cp
import numpy as np
import openpyxl


Demand_Excel = pd.read_excel('Input/Demands.xlsx',index_col=0)

data = pd.DataFrame(Demand_Excel)

wb = openpyxl.Workbook()

sheet = wb.active
sheet.cell(row=1, column=1).value = "총 수익"
sheet.cell(row=1, column=2).value = "그리드 판매 수익"
sheet.cell(row=1, column=3).value = "연료 가격"
sheet.cell(row=1, column=4).value = "건물 전기 절약값"
sheet.cell(row=1, column=5).value = "난방비 판매값"
sheet.cell(row=1, column=6).value = "전기차 전기 절약값"
sheet.cell(row=1, column=7).value = "수소차 탱크 절약값"
sheet.cell(row=1, column=8).value = "FC1 그리드 판매량"
sheet.cell(row=1, column=9).value = "FC1 건물 사용량"
sheet.cell(row=1, column=10).value = "FC1 전기차 사용량"
sheet.cell(row=1, column=11).value = "FC2 발전량"
sheet.cell(row=1, column=12).value = "CHP 발전량"
sheet.cell(row=1, column=13).value = "PV 그리드 판매량"
sheet.cell(row=1, column=14).value = "PV 건물 사용량"
sheet.cell(row=1, column=15).value = "PV 전기차 사용량"
#sheet.cell(row=1, column=1).value = ""
    
for i in range(0,2): # Time Regulation (0,8759)
    OeFC1g = cp.Variable()
    OeFC1b = cp.Variable()
    OeFC1e = cp.Variable()
    
    OeFC2 = cp.Variable()
    OeCHP = cp.Variable()
    
    OePVg = cp.Variable()
    OePVb = cp.Variable()
    OePVe = cp.Variable()
    
    BD = np.array(data.iloc[[i],[0]])
    EV = np.array(data.iloc[[i],[1]])
    HD = np.array(data.iloc[[i],[2]])
    
    PFC1 = 9.33863636
    PFC2 = 12.94857
    PCHP = 14.22143
    
    hFC1 = 0.50818182
    hFC2 = 0.393143
    hCHP = 2.297429
    
    HFC2 = 0.025714
    
    PGAS = 19.3870
    PP = np.array(data.iloc[[i],[3]])
    heat=396.76
    Hprice=8000
    
    constraints = [
    0<=OeFC1g,
    OeFC1g<=440,
    0<=OeFC1b,
    OeFC1b<=440,
    0<=OeFC1e,
    OeFC1e<=440,
    
    OeFC1g + OeFC1b + OeFC1e <= 440,
    
    0<=OePVg,
    OePVg<=50,
    0<=OePVb,
    OePVb<=50,
    0<=OePVe,
    OePVe<=50,
    
    OePVg + OePVb + OePVe == 50,
    
    
    0<=OeFC2,
    OeFC2<=350,
    0<=OeCHP,
    OeCHP<=140,
    
    OeFC1b + OePVb == BD,
    OeFC1e + OePVe == EV
    ]
    
    # Korean Money Unit Won
    heat_profit = ((OeFC1e + OeFC1g + OeFC1b) * hFC1 + OeFC2 * hFC2 +OeCHP * hCHP) * heat
    Power_sell = PP * (OeFC2 + OeCHP + OeFC1g + OePVg )
    Fuel_Price = ((OeFC1e + OeFC1g + OeFC1b) * PFC1 + OeFC2 * PFC2 +OeCHP * PCHP) * PGAS
    Building_profit = BD * 100 + 6160 # 100 is buying price for electric power, other is base price
    EV_profit = EV*100 + 2390 # 100 is buying price for electric power, other is base price
    H2_profit =  Hprice * ( OeFC2 * HFC2 - HD) # We have H2 tanks
    
    obj = cp.Maximize( Power_sell - Fuel_Price + EV_profit + H2_profit + Building_profit + heat_profit )
    prob = cp.Problem(obj, constraints)
    
    prob.solve() 
    
    sheet = wb.active
    print(OeFC1b.value)
    sheet.cell(row=i+2, column=1).value = obj.value
    sheet.cell(row=i+2, column=2).value = Power_sell[0][0].value
    sheet.cell(row=i+2, column=3).value = Fuel_Price.value
    sheet.cell(row=i+2, column=4).value = Building_profit[0][0]
    sheet.cell(row=i+2, column=5).value = heat_profit.value
    sheet.cell(row=i+2, column=6).value = EV_profit[0][0]
    sheet.cell(row=i+2, column=7).value = H2_profit[0][0].value
    sheet.cell(row=i+2, column=7).value = OeFC1b.value #error
#    sheet.cell(row=i+2, column=10).value = OeFC2.value


wb.save('Output/test_for_excel.xlsx')

I'm trying to extract my data into Excel, but I'm having a problem
in this part:

sheet.cell(row=i+2, column=7).value = OeFC1b.value

So I use print() and check the value. It is 50.20042375032.

I get this error message:

raise ValueError("Cannot convert {0!r} to Excel".format(value))  
ValueError: Cannot convert array(50.20042375) to Excel

Why is it an array and how do I extract that data?

Here is the whole code:

# -*- coding: utf-8 -*-
"""
Created on Sat Apr 16 18:18:01 2022

@author: mskim
"""

import pandas as pd
import cvxpy as cp
import numpy as np
import openpyxl


Demand_Excel = pd.read_excel('Input/Demands.xlsx',index_col=0)

data = pd.DataFrame(Demand_Excel)

wb = openpyxl.Workbook()

sheet = wb.active
sheet.cell(row=1, column=1).value = "총 수익"
sheet.cell(row=1, column=2).value = "그리드 판매 수익"
sheet.cell(row=1, column=3).value = "연료 가격"
sheet.cell(row=1, column=4).value = "건물 전기 절약값"
sheet.cell(row=1, column=5).value = "난방비 판매값"
sheet.cell(row=1, column=6).value = "전기차 전기 절약값"
sheet.cell(row=1, column=7).value = "수소차 탱크 절약값"
sheet.cell(row=1, column=8).value = "FC1 그리드 판매량"
sheet.cell(row=1, column=9).value = "FC1 건물 사용량"
sheet.cell(row=1, column=10).value = "FC1 전기차 사용량"
sheet.cell(row=1, column=11).value = "FC2 발전량"
sheet.cell(row=1, column=12).value = "CHP 발전량"
sheet.cell(row=1, column=13).value = "PV 그리드 판매량"
sheet.cell(row=1, column=14).value = "PV 건물 사용량"
sheet.cell(row=1, column=15).value = "PV 전기차 사용량"
#sheet.cell(row=1, column=1).value = ""
    
for i in range(0,2): # Time Regulation (0,8759)
    OeFC1g = cp.Variable()
    OeFC1b = cp.Variable()
    OeFC1e = cp.Variable()
    
    OeFC2 = cp.Variable()
    OeCHP = cp.Variable()
    
    OePVg = cp.Variable()
    OePVb = cp.Variable()
    OePVe = cp.Variable()
    
    BD = np.array(data.iloc[[i],[0]])
    EV = np.array(data.iloc[[i],[1]])
    HD = np.array(data.iloc[[i],[2]])
    
    PFC1 = 9.33863636
    PFC2 = 12.94857
    PCHP = 14.22143
    
    hFC1 = 0.50818182
    hFC2 = 0.393143
    hCHP = 2.297429
    
    HFC2 = 0.025714
    
    PGAS = 19.3870
    PP = np.array(data.iloc[[i],[3]])
    heat=396.76
    Hprice=8000
    
    constraints = [
    0<=OeFC1g,
    OeFC1g<=440,
    0<=OeFC1b,
    OeFC1b<=440,
    0<=OeFC1e,
    OeFC1e<=440,
    
    OeFC1g + OeFC1b + OeFC1e <= 440,
    
    0<=OePVg,
    OePVg<=50,
    0<=OePVb,
    OePVb<=50,
    0<=OePVe,
    OePVe<=50,
    
    OePVg + OePVb + OePVe == 50,
    
    
    0<=OeFC2,
    OeFC2<=350,
    0<=OeCHP,
    OeCHP<=140,
    
    OeFC1b + OePVb == BD,
    OeFC1e + OePVe == EV
    ]
    
    # Korean Money Unit Won
    heat_profit = ((OeFC1e + OeFC1g + OeFC1b) * hFC1 + OeFC2 * hFC2 +OeCHP * hCHP) * heat
    Power_sell = PP * (OeFC2 + OeCHP + OeFC1g + OePVg )
    Fuel_Price = ((OeFC1e + OeFC1g + OeFC1b) * PFC1 + OeFC2 * PFC2 +OeCHP * PCHP) * PGAS
    Building_profit = BD * 100 + 6160 # 100 is buying price for electric power, other is base price
    EV_profit = EV*100 + 2390 # 100 is buying price for electric power, other is base price
    H2_profit =  Hprice * ( OeFC2 * HFC2 - HD) # We have H2 tanks
    
    obj = cp.Maximize( Power_sell - Fuel_Price + EV_profit + H2_profit + Building_profit + heat_profit )
    prob = cp.Problem(obj, constraints)
    
    prob.solve() 
    
    sheet = wb.active
    print(OeFC1b.value)
    sheet.cell(row=i+2, column=1).value = obj.value
    sheet.cell(row=i+2, column=2).value = Power_sell[0][0].value
    sheet.cell(row=i+2, column=3).value = Fuel_Price.value
    sheet.cell(row=i+2, column=4).value = Building_profit[0][0]
    sheet.cell(row=i+2, column=5).value = heat_profit.value
    sheet.cell(row=i+2, column=6).value = EV_profit[0][0]
    sheet.cell(row=i+2, column=7).value = H2_profit[0][0].value
    sheet.cell(row=i+2, column=7).value = OeFC1b.value #error
#    sheet.cell(row=i+2, column=10).value = OeFC2.value


wb.save('Output/test_for_excel.xlsx')

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

糖粟与秋泊 2025-01-29 21:12:31

我不熟悉cvxpy lib,但我相信您正在尝试将列表写入Excel文档。我认为这是不支持的。

此代码产生类似的错误

import openpyxl
val = [50.20042375]
wb = openpyxl.Workbook()
sheet = wb.active
sheet.cell(row=1, column=1).value = val

来解决该问题,最终会添加[0]

import openpyxl
val = [50.20042375]
wb = openpyxl.Workbook()
sheet = wb.active
sheet.cell(row=1, column=1).value = val[0]

Im not familiar with the cvxpy lib but i belive you are trying to write a list to the excel document. I don't think that is supported.

This code produce a similar error

import openpyxl
val = [50.20042375]
wb = openpyxl.Workbook()
sheet = wb.active
sheet.cell(row=1, column=1).value = val

To fix the issue do add a [0] in the end like this

import openpyxl
val = [50.20042375]
wb = openpyxl.Workbook()
sheet = wb.active
sheet.cell(row=1, column=1).value = val[0]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文