如何从输出中删除字符 `b`?
How can I remove the character `b` from the output?
<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>
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
。
我怎样才能删除它?
删除行
data = data.encode('ascii')
xml.parsers.expat.ParserCreate
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')
<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>
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
。
我怎样才能删除它?
删除行
data = data.encode('ascii')
xml.parsers.expat.ParserCreate
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')