JOSN
How to count items in JSON data
Using JSON config files in Python
本文将整理:在 Python 中常用 JSON 操作。
import json # 字典 => 字符串,顺便美化输出 json_string = json.dumps({ "result":[{"find": "true"}] }, indent=2) # 字符串 => 字典 json_object = json.loads(json_string) # 文件 => 字典 json_ojbect = json.load(open("/path/to/file.txt")) # 求长度 len(json_object)
XML
xml.etree.ElementTree — The ElementTree XML API
Python xml ElementTree from a string source?
在 Python 中,与 XML 有关的操作,以及相关问题的解决办法。
# 导入模块 import xml.etree.ElementTree as ET # 读取XML数据 tree = ET.parse("/path/to/foo.xml") tree = ET.fromstring(xml_string) # 修改数据 for item in tree.iter("item"): item.text = "some text" tree.write("/path/to/bar.xml") # 打印XML数据 xml_string = tree.tosting()