json格式校驗
- 非嚴格模式
json.loads(data, strict=False)
如果strict為false(默認值為True),則字符串中允許使用控制字符。此上下文中的控制字符是那些字符代碼在0–31范圍內的字符,包括“\t”(制表符)、“\n”、“r”和“\0”。
代碼實例
import jsondef check_json(json_file) -> bool:try:# rb方式打開可以去除非utf-8編碼產生的錯誤with open(json_file, 'rb') as f:json.loads(f.read(), strict=False)return Trueexcept Exception as e:print(f"JSON format verification failed: {e}")return False
xml格式校驗
代碼實例
from xml.etree import ElementTree as ETdef check_xml(xml_file: str) -> bool:try:with open(xml_file, "r") as f:ET.fromstring(f.read())return Trueexcept Exception as e:print(f"JSON format verification failed: {e}")return False