Python 格式不正确(无效令牌)
试图从netcat命令返回的xml生成josn,返回的xml如下所示:Python 格式不正确(无效令牌),python,python-3.x,xml-parsing,elementtree,Python,Python 3.x,Xml Parsing,Elementtree,试图从netcat命令返回的xml生成josn,返回的xml如下所示: (b'<?xml version="1.0" encoding="UTF-8"?>\n<Reply Version="2020.005.021" Status="Passed">\n\t<FileDetails FileCount.DWD="2" NextFrom.QWD="608518
(b'<?xml version="1.0" encoding="UTF-8"?>\n<Reply Version="2020.005.021" Status="Passed">\n\t<FileDetails FileCount.DWD="2" NextFrom.QWD="608518" ExitCode.DWD="0">\n\t\t<File Guid="cbcb8339_Feed_ PL World wk6.mxf" VolumeName="000121L5" ArchiveNumber.DWD="7" VolumeGroup="MAIN" FileSize.QWD="36114564233" ArchiveDate="2013/2/14 20:44:50" LastRestoreDate="2018/5/2 12:32:24" RestoreCount.DWD="2" Status="OFFLINE"/>\n\t\t<File Guid="cbcb8339_Feed_ PL World wk6.mxf" VolumeName="000037L7" ArchiveNumber.DWD="31" VolumeGroup="Fodbold_L7" FileSize.QWD="36114564233" ChangerDetail="3 IBM-TS3510" ChangerPosition="Slot 99" ArchiveDate="2018/5/7 08:23:18" LastRestoreDate="2021/4/2 18:16:35" RestoreCount.DWD="3" Status="ONLINE"/>\n\t</FileDetails>\n</Reply>\x00', None)
xmlstr = '<?xml version="1.0" encoding="UTF-8"?><Reply Version="2020.005.021" Status="Passed"><FileDetails FileCount.DWD="2" NextFrom.QWD="608518" ExitCode.DWD="0"><File Guid="cbcb8339_Feed_ PL World wk6.mxf" VolumeName="000121L5" ArchiveNumber.DWD="7" VolumeGroup="MAIN" FileSize.QWD="36114564233" ArchiveDate="2013/2/14 20:44:50" LastRestoreDate="2018/5/2 12:32:24" RestoreCount.DWD="2" Status="OFFLINE"/><File Guid="cbcb8339_Feed_ PL World wk6.mxf" VolumeName="000037L7" ArchiveNumber.DWD="31" VolumeGroup="Fodbold_L7" FileSize.QWD="36114564233" ChangerDetail="3 IBM-TS3510" ChangerPosition="Slot 99" ArchiveDate="2018/5/7 08:23:18" LastRestoreDate="2021/4/2 18:16:35" RestoreCount.DWD="3" Status="ONLINE"/></FileDetails></Reply>'
import xml.etree.ElementTree as ET
root = ET.fromstring(xmlstr)
for element in root.iter("*"):
print(element)
当我运行上述代码时,出现以下错误:
xml.etree.ElementTree.ParseError: not well-formed (invalid token): line 7, column 8
但是,如果我打印出xmlstr
,输出看起来很好:
<?xml version="1.0" encoding="UTF-8"?>
<Reply Version="2020.005.021" Status="Passed">
<FileDetails FileCount.DWD="2" NextFrom.QWD="608518" ExitCode.DWD="0">
<File Guid="cbcb8339_Feed_ PL World wk6.mxf" VolumeName="000121L5" ArchiveNumber.DWD="7" VolumeGroup="MAIN" FileSize.QWD="36114564233" ArchiveDate="2013/2/14 20:44:50" LastRestoreDate="2018/5/2 12:32:24" RestoreCount.DWD="2" Status="OFFLINE"/>
<File Guid="cbcb8339_Feed_ PL World wk6.mxf" VolumeName="000037L7" ArchiveNumber.DWD="31" VolumeGroup="Fodbold_L7" FileSize.QWD="36114564233" ChangerDetail="3 IBM-TS3510" ChangerPosition="Slot 99" ArchiveDate="2018/5/7 08:23:18" LastRestoreDate="2021/4/2 18:16:35" RestoreCount.DWD="3" Status="ONLINE"/>
</FileDetails>
</Reply>
如果我将xml作为如下字符串传递,它也会起作用:
(b'<?xml version="1.0" encoding="UTF-8"?>\n<Reply Version="2020.005.021" Status="Passed">\n\t<FileDetails FileCount.DWD="2" NextFrom.QWD="608518" ExitCode.DWD="0">\n\t\t<File Guid="cbcb8339_Feed_ PL World wk6.mxf" VolumeName="000121L5" ArchiveNumber.DWD="7" VolumeGroup="MAIN" FileSize.QWD="36114564233" ArchiveDate="2013/2/14 20:44:50" LastRestoreDate="2018/5/2 12:32:24" RestoreCount.DWD="2" Status="OFFLINE"/>\n\t\t<File Guid="cbcb8339_Feed_ PL World wk6.mxf" VolumeName="000037L7" ArchiveNumber.DWD="31" VolumeGroup="Fodbold_L7" FileSize.QWD="36114564233" ChangerDetail="3 IBM-TS3510" ChangerPosition="Slot 99" ArchiveDate="2018/5/7 08:23:18" LastRestoreDate="2021/4/2 18:16:35" RestoreCount.DWD="3" Status="ONLINE"/>\n\t</FileDetails>\n</Reply>\x00', None)
xmlstr = '<?xml version="1.0" encoding="UTF-8"?><Reply Version="2020.005.021" Status="Passed"><FileDetails FileCount.DWD="2" NextFrom.QWD="608518" ExitCode.DWD="0"><File Guid="cbcb8339_Feed_ PL World wk6.mxf" VolumeName="000121L5" ArchiveNumber.DWD="7" VolumeGroup="MAIN" FileSize.QWD="36114564233" ArchiveDate="2013/2/14 20:44:50" LastRestoreDate="2018/5/2 12:32:24" RestoreCount.DWD="2" Status="OFFLINE"/><File Guid="cbcb8339_Feed_ PL World wk6.mxf" VolumeName="000037L7" ArchiveNumber.DWD="31" VolumeGroup="Fodbold_L7" FileSize.QWD="36114564233" ChangerDetail="3 IBM-TS3510" ChangerPosition="Slot 99" ArchiveDate="2018/5/7 08:23:18" LastRestoreDate="2021/4/2 18:16:35" RestoreCount.DWD="3" Status="ONLINE"/></FileDetails></Reply>'
import xml.etree.ElementTree as ET
root = ET.fromstring(xmlstr)
for element in root.iter("*"):
print(element)
xmlstr=''
将xml.etree.ElementTree作为ET导入
root=ET.fromstring(xmlstr)
对于root.iter(“*”)中的元素:
打印(元素)
我做错什么了吗