如何从输出中删除字符“b”?

2024-06-02 08:04:22 发布

您现在位置:Python中文网/ 问答频道 /正文

 <member>
    <detaileddescription>
        Hello
        <formula id="39">my</formula>
        name
        <formula id="102">is</formula>
        Buddy.
        <formula id="103">I</formula>
        am a
        <itemizedlist>
            <listitem>
            superhero
            <formula id="104">.</formula>
            </listitem>
            <listitem>
                At least,
                <formula id="105">I think</formula>
            </listitem>
        </itemizedlist>
        so...:)
        <simplesect kind="see">
            What
            <ref refid="ref_id" kindref="ref_kindref">do you</ref>
            <bold>think</bold> ?
        </simplesect>
        Let me know. :)
    </detaileddescription>
 </member>

following source code工程:

from xml.parsers import expat

xmlFile = "xml.xml"


class xmlText(object):
    def __init__(self):
        self.textBuff = ""

    def CharacterData(self, data):
        data = data.strip()
        if data:
            data = data.encode('ascii')
            self.textBuff += str(data) + "\n"

    def Parse(self, fName):
        xmlParser = expat.ParserCreate()
        xmlParser.CharacterDataHandler = self.CharacterData
        xmlParser.Parse(open(fName).read(), 1)


xText = xmlText()
xText.Parse(xmlFile)
print("Text from %s\n=" % xmlFile)
print(xText.textBuff)

输出

Text from xml.xml
=
b'Hello'
b'my'
b'name'
b'is'
b'Buddy.'
b'I'
b'am a'
b'superhero'
b'.'
b'At least,'
b'I think'
b'so...:)'
b'What'
b'do you'
b'think'
b'?'
b'Let me know. :)'

但是,它会在每行文本前面添加一个字符b

我怎样才能去掉它


Tags: fromselfrefiddataparsedefxml
2条回答

拆下线路

data = data.encode('ascii')

^{}

xml.parsers.expat.ParserCreate(encoding=None, namespace_separator=None)

Creates and returns a new xmlparser object. encoding, if specified, must be a string naming the encoding used by the XML data. Expat doesn’t support as many encodings as Python does, and its repertoire of encodings can’t be extended; it supports UTF-8, UTF->16, ISO-8859-1 (Latin1), and ASCII.

只需将编码传递给Parse方法内的ParserCreate函数即可

xmlParser = expat.ParserCreate(encoding='ASCII')

相关问题 更多 >