您好,我尝试在visual studio上用c#运行python代码。但是当我运行这段代码时,我得到了一个错误“ironpython.runtime.unboundnameexception:'名称'true'未定义'”。我对这些东西(ironpython和用c#运行python代码)还不熟悉,所以很抱歉,从现在开始我的演示很糟糕。这就是我的c代码:
private static void Option2_IronPython()
{
var engine = Python.CreateEngine();
var script = @"C:\users\Toshiba\SUPPLYDEMMAND.ipynb";
var source = engine.CreateScriptSourceFromFile(script);
var argv = new List<string>();
argv.Add("");
engine.GetSysModule().SetVariable("argv", argv);
var eIO = engine.Runtime.IO;
var errors = new MemoryStream();
eIO.SetErrorOutput(errors, Encoding.Default);
var results = new MemoryStream();
eIO.SetOutput(results, Encoding.Default);
var scope = engine.CreateScope();
source.Execute(scope);
string str(byte[] x) => Encoding.Default.GetString(x);
Console.WriteLine("Errors:");
Console.WriteLine(str(errors.ToArray()));
Console.WriteLine();
Console.WriteLine("Results");
Console.WriteLine(str(results.ToArray()));
}
以下是错误的详细信息:
Errors:
Traceback (most recent call last):
File "C:\Users\Toshiba\SUPPLYDEMMAND.ipynb", line 7, in <module>
"scrolled": true
NameError: name 'true' is not defined
下面是我的python代码:
# !/usr/bin/env python
# -*- coding: utf-8 -*-
import pandas as pd
import requests as req
import dateutil as pdate
import transparency
from transparency.electricty.markets import gop
import datetime
import time
x = datetime.datetime.now()
today = datetime.date.today()
tomorrow = today + datetime.timedelta(days = 1)
tomorrow = str(tomorrow)
supply_demmand = gop.supply_demmand_curve(date=tomorrow)
col_name="Price"
first_col = supply_demmand.pop(col_name)
supply_demmand.insert(0, col_name, first_col)
grouped = supply_demmand.groupby(supply_demmand.Hour)
df_list = ['df'+str(i) for i in range(0,24)]
df_list[0] = grouped.get_group(0)
df_list[0] = df_list[0].drop(columns=['Hour'])
for j in range (1,24):
df_list[j] = grouped.get_group(j)
df_list[j]= df_list[j].drop(columns=['Hour'])
df_list[j]= df_list[j].reset_index(drop=True)
df_list[0]= df_list[0].reset_index(drop=True )
df_list[0]= pd.concat([df_list[0],df_list[j]], axis=1)
df_list[0]["Supply"]= df_list[0]["Supply"] * -1
path = "Supply_Demmand.xlsx"
writer = pd.ExcelWriter(path, engine = 'openpyxl')
df_list[0].to_excel(writer,sheet_name = tomorrow)
writer.save()
事实上,我把这些代码的一部分从土耳其语翻译成了英语,所以如果你试图在ide上运行,你会得到错误。我只是想知道这个错误的逻辑,你能给我解释一下吗?谢谢你的帮助。
暂无答案!
目前还没有任何答案,快来回答吧!