我有如下xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<Main xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:gml="http://www.opengis.net/gml/3.2" xmlns="http://cnig.gouv.fr/pcrs" gml:id="PlanCorpsRueSimplifie.1" version="2.0">
<gml:boundedBy>
</gml:boundedBy>
<featureMember>
<EmpriseEchangePCRS gml:id="EmpriseEchangePCRS.12189894">
<datePublication>2020-05-13</datePublication>
<type>Cellules</type>
<geometrie>
<gml:MultiSurface gml:id="EmpriseEchangePCRS.12189894-0" srsName="EPSG:3944" srsDimension="3">
<gml:surfaceMember>
<gml:Surface gml:id="EmpriseEchangePCRS.12189894-1">
<gml:patches>
</gml:patches>
</gml:Surface>
我想把这个文件转换成json文件。 我试过了,但我总是犯同样的错误:
import xmltodict
import xml.etree.ElementTree as ET
root = ET.fromstring(open('JeuxTestv2.gml').read())
print(xmltodict.parse(root)['Main'])
错误:
Traceback (most recent call last):
File "C:\Users\xmltodict.py", line 6, in <module>
print(xmltodict.parse(root)['Main'])
File "C:\Users\xmltodict.py", line 327, in parse
parser.Parse(xml_input, True)
TypeError: a bytes-like object is required, not 'xml.etree.ElementTree.Element'
我正在使用Python 3.7.6
当我尝试时,ET.fromstring()将解析已经以字符串格式表示的XML
此示例将显示以下错误
我使用ET.tostring()生成XML数据的字符串表示,它可以用作xmltodict.parse()的有效参数Click这里是ET.tostring()文档
下面的代码将解析XML文件并生成JSON文件。我使用了自己的XML示例。确保所有XML标记都已正确关闭
XML:
PYTHON代码:
输出: 上述代码将创建以下JSON文件:
相关问题 更多 >
编程相关推荐