使用dicttoxml modu从字典生成时,xml字符串用b'<xml\u string>括起来

2024-09-30 07:30:53 发布

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

我使用dicttoxml模块将字典转换为xml。在

代码:

cfg_dict = { 'mobile' :
                { 'checkBox_OS' :
                  { 'status' : 'None', 
                    'radioButton_Andriod' :
                      { 'status' : 'None',
                        'comboBox_Andriod_Brands' : 'LG'},
                    'radioButton_Windows' :
                      { 'status' : 'None',
                        'comboBox_Windows_Brands' : 'Nokia'},
                    'radioButton_Others' :
                      { 'status' : 'None',
                        'comboBox_Others_Brands' : 'Apple'}},
                  'checkBox_Screen_size' :
                    { 'status' : 'None',
                      'doubleSpinBox_Screen_size' : '5.0' }}
              }        

from dicttoxml import dicttoxml
xml = dicttoxml(self.cfg_dict)
print (xml)

输出:

^{pr2}$

我不知道它为什么用b'括起来。如何在没有这个b“”的情况下生成xml字符串?在

浏览器在打开包含此内容的xml文件时也会发出错误消息。在


Tags: nonesizewindowsstatusxmlcfgscreendict
2条回答

这是python3中非Unicode字符串的正常表示。在Python shell中尝试以下操作:

>>> type("foo")
<class 'str'>
>>> type(b"foo")
<class 'bytes'>
>>> type("Rübe")
<class 'str'>
>>> type(b"Rübe")
  File "<stdin>", line 1
SyntaxError: bytes can only contain ASCII literal characters.

所以一切都好。你没问题。在

另请参见^{}^{}。在

编辑:

看看编码和解码是如何工作的。在

^{pr2}$

所以只要使用my_byte_string.decode(my_encoding),其中{}可能是{}。在

这里是图书馆的作者。在

看起来您使用的是python3。Python3以二进制格式存储字符串,除非指定编码。在

{{1>将字符串{1>转换为cd2>字符串的示例:

>>> xml_string = xml.decode('utf-8')
>>> print(xml_string)
<?xml version="1.0" encoding="UTF-8" ?><root><mobile type="dict"><checkBox_OS type="dict"><radioButton_Windows type="dict"><status type="str">None</status><comboBox_Windows_Brands type="str">Nokia</comboBox_Windows_Brands></radioButton_Windows><radioButton_Others type="dict"><comboBox_Others_Brands type="str">Apple</comboBox_Others_Brands><status type="str">None</status></radioButton_Others><status type="str">None</status><radioButton_Andriod type="dict"><comboBox_Andriod_Brands type="str">LG</comboBox_Andriod_Brands><status type="str">None</status></radioButton_Andriod></checkBox_OS><checkBox_Screen_size type="dict"><status type="str">None</status><doubleSpinBox_Screen_size type="str">5.0</doubleSpinBox_Screen_size></checkBox_Screen_size></mobile></root>

干杯!在

相关问题 更多 >

    热门问题