数据表传递到IronPython的 [英] Passing DataTable to IronPython

查看:452
本文介绍了数据表传递到IronPython的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个.NET项目中,我使用IronPython的进行数据的一些处理。在present,在C#code遍历并产生一个IronPython的脚本的每一行和列,需要动态计算。不过,我想使这个过程更加高效传递的DataTable对象和脚本来反对执行。不幸的是,我收到数据表对象是unsubscriptable 错误并没有处理发生。

I have a .NET project where I am using IronPython to perform some processing of the data. At present, the C# code loops through and generates an IronPython script for each row and column that requires dynamic calculation. However, I'd like to make the process more efficient by passing in the DataTable object and a script to execute against it. Unfortunately, I'm getting 'DataTable' object is unsubscriptable error and no processing takes place.

C#代码段生成并执行IronPython的脚本:

DataTable dt = new DataTable();
dt.Columns.Add("count", typeof(int));
dt.Columns.Add("calc", typeof(int));
dt.Rows.Add(1, null);

ScriptEngine engine = Python.CreateEngine(options);
ScriptScope scope = engine.CreateScope();
scope.SetVariable("dt", dt);
ScriptSource source = engine.CreateScriptSourceFromString(code, SourceCodeKind.AutoDetect);
object result = source.Execute(scope);
DataTable table = scope.GetVariable<System.Data.DataTable>("dt");

Python脚本要对表执行的:

import clr
clr.AddReference('System.Data')
from System import Data
from System.Data import DataTable
for row in dt.Rows:
    dt["calc"] = dt["count"] + 1

我如何修复数据表对象是unsubscriptable 错误,或者有没有更好的方式来处理传递一个DataTable到IronPython的?

How do I fix the 'DataTable' object is unsubscriptable error, or is there a better way to handle passing a DataTable into IronPython?

推荐答案

DataTable对象真正的的unsubscriptable,即无法通过索引的属性来访问(即 DT [指数] )。

DataTable object really is unsubscriptable, i.e. cannot be accessed through indexed properties (i.e. dt[index] ).

您大概意思是:

for row in dt.Rows:
    row["calc"] = row["count"] + 1

在我替换 DT 行变量

这篇关于数据表传递到IronPython的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆