Python-LXML:ValueError:所有字符串必须与XML兼容

时间:2017-10-22 05:45:17

标签: python unicode encoding lxml

当尝试使用python代码生成xml时,我收到此错误:

  

ValueError:所有字符串必须兼容XML:Unicode或ASCII,无NULL字节或控制字符。

请帮助我完成我的代码。我基本上有两个问题。

第一个代码:打开python IDLE

# -*- coding: cp1252 -*-

def generatexmlfile():
        from lxml import etree as ET            
        page = ET.Element('Module')
        ET.tostring(page, xml_declaration=True)
        doc = ET.ElementTree(page)
        #headElt = ET.SubElement(page, 'head')
        headElt = ET.SubElement(page, 'TestCaseName', NAME="118_Function Sinitiation ", TCID="118", AUTOMATIONID="118_01")
        title = ET.SubElement(headElt, 'PAYMENT_value')
        input_string='Suspended|€ 2,000.00'        
        cleaned_string = ''.join(c for c in input_string if valid_xml_char_ordinal(c))
        title.text = cleaned_string              
        outFile = open('D:\\python\\CreateXML\\homemade.xml', 'w')
        doc.write(outFile,xml_declaration=True,encoding='utf-8',method="xml")

def valid_xml_char_ordinal(c):
    codepoint = ord(c)
    # conditions ordered by presumed frequency
    return (
        0x20 <= codepoint <= 0xD7FF or
        codepoint in (0x9, 0xA, 0xD) or
        0xE000 <= codepoint <= 0xFFFD or
        0x10000 <= codepoint <= 0x10FFFF
        )

generatexmlfile()

问题是:

  1. 除非添加# -*- coding: cp1252 -*-,否则IDLE不允许我保存。什么是# -*- coding: cp1252 -*-

  2. 代码中的€字符会导致问题。请让我知道如何处理它并完成xml生成

0 个答案:

没有答案