我使用口授模块将字典转换为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)输出:
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文件时也会发出错误消息。
发布于 2014-11-19 16:41:07
这是图书馆的作者。
除非指定编码,否则似乎使用Python3.Python 3以二进制格式存储字符串。
继续使用示例代码,若要将xml从字节字符串转换为字符串字符,请使用decode方法:
>>> 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>干杯!
https://stackoverflow.com/questions/27019740
复制相似问题