首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >xml字符串用b'<xml_ string >‘括在一起,同时使用dicttoxml模块从字典中生成

xml字符串用b'<xml_ string >‘括在一起,同时使用dicttoxml模块从字典中生成
EN

Stack Overflow用户
提问于 2014-11-19 14:48:32
回答 1查看 3.5K关注 0票数 2

我使用口授模块将字典转换为xml。

代码:

代码语言:javascript
复制
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)

输出:

代码语言:javascript
复制
b'<?xml version="1.0" encoding="UTF-8" ?><root><mobile type="dict"><checkBox_OS type="dict"><radioButton_Andriod type="dict"><status type="bool">false</status><comboBox_Andriod_Brands type="str">Sony</comboBox_Andriod_Brands></radioButton_Andriod><radioButton_Windows type="dict"><status type="bool">false</status><comboBox_Windows_Brands type="str">HTC</comboBox_Windows_Brands></radioButton_Windows><status type="bool">false</status><radioButton_Others type="dict"><status type="bool">false</status><comboBox_Others_Brands type="str">Apple</comboBox_Others_Brands></radioButton_Others></checkBox_OS><checkBox_Screen_size type="dict"><doubleSpinBox_Screen_size type="float">5.0</doubleSpinBox_Screen_size><status type="bool">false</status></checkBox_Screen_size></mobile></root>'

我不知道为什么附在b‘’里面。如何在没有此b'‘的情况下生成xml字符串?

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

EN

回答 1

Stack Overflow用户

发布于 2014-11-19 16:41:07

这是图书馆的作者。

除非指定编码,否则似乎使用Python3.Python 3以二进制格式存储字符串。

继续使用示例代码,若要将xml从字节字符串转换为字符串字符,请使用decode方法:

代码语言:javascript
复制
>>> 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>

干杯!

票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27019740

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档