Ironpython& C#:没有名为.....的模块 [英] Ironpython & C# : no module named .....

查看:103
本文介绍了Ironpython& C#:没有名为.....的模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello All,



我目前正在尝试在c#中集成python脚本。此脚本的目标是解析xml数据。我在下面给出了我的代码。所以每当我运行项目时,我都会得到以下错误:错误:没有名为xml.parsers.expat的模块

如果我使用minidom / ElementTree错误将是相同的



Program.cs

----------

Hello All,

I'm currently trying to integrate python script in c#. Goal of this script is to parse the xml data. I have given my code below. So whenever I run the project I get below error:Error:No module named xml.parsers.expat
The error will be same if I use minidom/ElementTree

Program.cs
----------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using IronPython.Hosting;
using Microsoft.Scripting.Hosting;
using IronPython.Modules;
using IronPython.Runtime;
using IronPython.Compiler;
using IronPython.Zlib;
using IronPython.SQLite;

namespace PyCsharpApp3
{
    class Program
    {
        static void Main(string[] args)
        {
            var ipy = Python.CreateRuntime();
            dynamic test = ipy.UseFile("Test.py");
            test.start_element();
            Console.ReadLine();
        }
    }
}





test.py

- -------



test.py
--------

import xml.parsers.expat


# 3 handler functions
def start_element(name, attrs):
    print('Start element:', name, attrs)
def end_element(name):
    print('End element:', name)
def char_data(data):
    print('Character data:', repr(data))

p = xml.parsers.expat.ParserCreate()

p.StartElementHandler = start_element
p.EndElementHandler = end_element
p.CharacterDataHandler = char_data

p.Parse("""<?xml version="1.0"?>
<parent id="top"><child1 name="paul">Text goes here</child1>
<child2 name="fred">More text</child2>
</parent>""", 1)





我尝试了什么:



推荐此链接如何使用Parse和Import解决IronPython中的异常? [ ^ ]



但仍面临同样的问题。请帮助



What I have tried:

Referred this link How solve exception in IronPython with Parse and Import?[^]

But still facing the same issue. Please help

推荐答案

IronPython实现没有附带 xml.parsers.expat 。但是,有一种解决方法。有一个项目 FePy [ ^ ]为IronPython提供增强功能。而xml.parsers.expat就是其中之一。下载此expat实施文件 [ ^ ]并将其复制到您的IronPython安装文件夹中的Lib\xml\parsers\expat.py
The IronPython implementation does not come with xml.parsers.expat. However, there is a workaround. There is a project FePy[^] that provides enhancements for IronPython. And xml.parsers.expat is one of them. Download this expat implementation file[^] and copy it to Lib\xml\parsers\expat.py in your IronPython installation folder.


这篇关于Ironpython&amp; C#:没有名为.....的模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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