gpt4 book ai didi

c# - 从 c# 程序错误 : No Module named xml. etree.cElementTree 调用 python 脚本

转载 作者:数据小太阳 更新时间:2023-10-29 02:44:05 26 4
gpt4 key购买 nike

我写了一个 python 脚本来解析一个 xml 文件。我从 C# 项目调用此文件。但是在运行程序时出现错误:没有名为 xml.etree.cElementTree 的模块。

Program.cs
-----------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using IronPython.Hosting;
using IronPython.Modules;

namespace RunExternalScript
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Press enter to execute the python script!");
Console.ReadLine();

var py = Python.CreateEngine();
try
{
py.ExecuteFile("wg.py");
}
catch (Exception ex)
{
Console.WriteLine(
"Oops! We couldn't execute the script because of an exception: " + ex.Message);
}

Console.WriteLine("Press enter to exit...");
Console.ReadLine();
}
}
}



wg.py
-----


import xml.etree.cElementTree as ET

tree = ET.parse('Wg.xml')
root = tree.getroot()
childrens = root.getchildren()


for p in root.findall('WGTemplate'):
name = p.find('TemplateName').text
# print(name)
loc = p.find('Filename').text
# print(loc)
for p1 in p.findall('Product'):
print("{:<50}{:<50}{:>50}".format(name, loc, p1.text))

注意:没有名为'xml'的文件夹或文件

最佳答案

设置你的python路径,知道你的系统路径在 .py 文件中输入

import sys
print(sys.path)

有了这个你会得到你的系统路径并在

中设置所有路径
ScriptEngine engine = Python.CreateEngine(); //For Engine to initiate the script
List<string> pathes = engine.GetSearchPaths().ToList();
pathes.AddRange(new[]
{
@"<add your all path here>"
});
engine.SetSearchPaths(pathes);

希望我的回答能帮助您解决问题。

关于c# - 从 c# 程序错误 : No Module named xml. etree.cElementTree 调用 python 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41280418/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com