def to_xml(self):
if self.format is not None:
tag = Element('data', attrib={'format': self.format})
else:
tag = Element('data')
if self.format == 'hex':
tag.text = self.data.encode('hex')
else: # asciic case
encoded = ''
for c in self.data:
if c in _PRINTABLE:
encoded += c
elif c in ('\n', '\r', '\t'):
encoded += {
'\n': '\\n',
'\r': '\\r',
'\t': '\\t',
}[c]
else:
encoded += '\\x{:02x}'.format(ord(c))
tag.text = encoded
return tag
评论列表
文章目录