简体   繁体   English

python lxml与py2exe

[英]python lxml with py2exe

I have Generated an XML with dom and i want to use lxml to pretty print the xml. 我用dom生成了一个XML,我想使用lxml来打印xml。

this is my code for pretty print the xml 这是我的代码,用于打印xml

def prettify_xml(xml_str):

   import lxml.etree as etree
   root = etree.fromstring(xml_str)
   xml_str =  etree.tostring(root, pretty_print=True)

   return xml_str

my output should be an xml formatted string. 我的输出应该是一个xml格式的字符串。

I got this code from some post in stactoverflow. 我从stactoverflow中的一些帖子中得到了这段代码。 This works flawlessly when i am compiling wit python itself. 当我编译机智python本身时,这完美无缺。 But when i convert my project to a binary created from py2exe (my binary is windows service with a namedpipe).I had two problems: 但是当我将我的项目转换为从py2exe创建的二进制文件时(我的二进制文件是带有namedpipe的windows服务)。我有两个问题:

  1. My service was not starting , i solved this by adding lxml.etree in includes option in py2exe function. 我的服务没有启动,我通过在py2exe函数中添加了includes选项的lxml.etree解决了这个问题。 then on my service started properly. 然后我的服务正常启动。

  2. when xml generation in called here, is the error which I am seeing in my log 'module' object has no attribute 'fromstring' 当在这里调用xml生成时,我在日志'module' object has no attribute 'fromstring'看到的错误是'module' object has no attribute 'fromstring'

where do i rectify this error ? 我在哪里纠正这个错误? And Is my first problem's solution correct ? 我的第一个问题解决方案是否正确?

my xml generation Code : 我的xml代码:

from xml.etree import ElementTree
from xml.dom import minidom
from xml.etree.ElementTree import Element, SubElement, tostring, XML 
import lxml.etree 


    def prettify_xml(xml_str):

      root = lxml.etree.fromstring(xml_str)
      xml_str =  lxml.etree.tostring(root, pretty_print=True)

      return xml_str

   def dll_xml(status):
    try:
        xml_declaration = '<?xml version="1.0" standalone="no" ?>'

        rootTagName='response'
        root = Element(rootTagName)
        root.set('id' , 'rp001')

        parent = SubElement(root, 'command', opcode ='-ac')

        # Create children
        chdtag1Name = 'mode'
        chdtag1Value = 'repreport'

        chdtag2Name='status'
        chdtag2Value = status

        fullchildtag1 = ''+chdtag1Name+' value = "'+chdtag1Value+'"'
        fullchildtag2=''+chdtag2Name+' value="'+chdtag2Value+'"'

        children = XML('''<root><'''+fullchildtag1+''' /><'''+fullchildtag2+'''/></root> ''')

        # Add parent
        parent.extend(children)
        dll_xml_doc = xml_declaration + tostring(root)

        dll_xml_doc = prettify_xml(dll_xml_doc)

        return dll_xml_doc

    except Exception , error:
       log.error("xml_generation_failed : %s" % error)

Try to use PyInstaller instead py2exe. 尝试使用PyInstaller而不是py2exe。 I converted your program to binary .exe with no problem just by running python pyinstaller.py YourPath\\xml_a.py . 我只是通过运行python pyinstaller.py YourPath\\xml_a.py将程序转换为二进制.exe。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM