我尝试从我的 json 文件中读取数据。首先,我将我的 xml 文件转换为 json,然后输出这个 json 文件,例如:
[
{
"ABC": {
"idObjet": "AffleurantEnveloppePCRS.0",
"nature": "03",
"precisionPlanimetrique": "010",
},
"reseau": "DECH",
"thematique": "10"
},
"xmlns:fn": "http://www.w3.org/2005/xpath-functions",
},
{
"DEF": {
"enveloppe": {
"xlink:href": "GEOMETRIE.ENV.AffleurantEnveloppePCRS.0"
},
"gml:id": "GEOMETRIE.AffleurantEnveloppePCRS.0"
},
"xmlns:fn": "http://www.w3.org/2005/xpath-functions",
}
]
然后我尝试从这个文件中打印一些数据。我json.loads()
用来读取这些数据:
import xmltodict
import json
dict = xmltodict.parse(open('Testv2.gml').read(), attr_prefix='')
dict3 = json.loads('test.json', strict=False)
print(dict3.keys())
with open('test.json', "wt", encoding='utf-8', errors='ignore') as f:
json.dump(dict, f, indent=4, sort_keys=True)
我尝试只打印键(ABC 和 DEF)
错误 :
Traceback (most recent call last):
File "C:/test.py", line 7, in <module>
dict3 = json.loads('test.json', strict=False)
File "C:\AppData\Local\Programs\Python\Python38-32\lib\json\__init__.py", line 370, in loads
return cls(**kw).decode(s)
File "C:\AppData\Local\Programs\Python\Python38-32\lib\json\decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "C:\AppData\Local\Programs\Python\Python38-32\lib\json\decoder.py", line 355, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)