json语法:
object {string:value,...}
value string/number/object/array/true/false/null
array value
具体http://json.org/
举例:
{ "yuzusoft": { "birthYear": "2006", "cross":[ "こぶいち", "むりりん" ], "country":"japan", "job":"galgame", "works":[ "天神乱漫", "dracu-riot!" ] }, "favorite":{ "birthYear":"2004", "cross":["司田カズヒ","GT","なつめえり"], "country":"japan", "job":"galgame", "works":[ "星空のメモリア", "いろとりどりのセカイ", "いろとりどりのヒカリ" ] } }
json只支持双引号
python的json模块
def load(fp, cls=None, object_hook=None, parse_float=None, parse_int=None, parse_constant=None, object_pairs_hook=None, **kw) #load从文件流中读 def loads(s, encoding=None, cls=None, object_hook=None, parse_float=None, parse_int=None, parse_constant=None, object_pairs_hook=None, **kw) #loads可以读作load string,从字符串读取,转成列表或字典 def dumps(obj, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, cls=None, indent=None, separators=None, default=None, sort_keys=False, **kw) #dumps可以理解为dump to string,对象转成字符串,对象可以是字典,列表等 #ensure_ascii 如果对象中有中文或日文,要定义ensure_ascii=False def dump(obj, fp, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, cls=None, indent=None, separators=None, default=None, sort_keys=False, **kw) #dump可以理解为dump to file
import json temp={} with open("Read.json","r",encoding="utf-8") as Rstream: temp=json.loads(Rstream.read()) print(temp) with open("Write.json","w",encoding="utf-8") as Wstream: json.dump(temp,Wstream,ensure_ascii=False)
pickle模块
class TempClass(object): def __init__(self): self.show() def show(self): print("This is the text Class") temp=TempClass() with open("Read.pkl","wb") as Wstream: pickle.dump(temp,Wstream) with open("Read.pkl","rb") as Wstream: temp2=pickle.load(Wstream) print(temp2.show())
xml语法:
xml开头要有<?xml version="1.0" encoding="UTF-8" ?>
xml从一个根元素开始,就像linux的目录结构一样
xml的根节点,子节点,标签,属性,值
<?xml version="1.0" encoding="UTF-8" ?> <galgame_company> <yuzusoft> <birthYear>2006</birthYear> <cross_group> <cross1>こぶいち</cross1> <cross2>むりりん</cross2> </cross_group> <country>japan</country> <job>galgame</job> <works_group> <works1>天神乱漫</works1> <works1>dracu-riot!</works1> </works_group> </yuzusoft> <favorite> <birthYear>2004</birthYear> <cross_group> <cross1>司田カズヒ</cross1> <cross2>GT</cross2> <cross3>なつめえり</cross3> </cross_group> <country>japan</country> <job>galgame</job> <works_group> <works>星空のメモリア</works> <works>いろとりどりのセカイ</works> <works>いろとりどりのヒカリ</works> </works_group> </favorite> </galgame_company>
xml模块:
思路:首先要把xml从文件中解析出来,然后从找到根节点,然后就能往下读
修改:找到标签,修改属性,修改值,添加删除标签
创建:先创建根节点,然后再根节点上创建标签
import xml.etree.ElementTree as ET xmlTree=ET.parse("Read.xml") #先解析文件 root=xmlTree.getroot() #拿到根节点 #遍历节点 for company in root: print("-->",company.tag,company.attrib) for item in company: print("\t-->",item.tag)
configparse模块:
[client]
port = 3306
socket = /data/3306/mysql.sock
[mysqld]
user = mysql
port = 3306
socket = /data/3306/mysql.sock
basedir = /application/mysql
datadir = /data/3306/data
查看
读(最重要):
import configparser #定义configparse对象 config=configparser.ConfigParser() config.read("my.cnf") print(config.sections()) for section in config: #遍历section print(section) for key in config[section]: #遍历key print("--->%s=%s"%(key,config[section][key]))
写:
#定义configparse对象 config=configparser.ConfigParser() config['client']={"port":"3306","socket":"/data/3306/mysql.sock"} config['mysqld']={"user":"mysql","port":3306,"socket":"/data/3306/mysql.sock"} config['mysqldump']={"max_allowed_packet":"2M"} with open("my,cnf","w") as Wtf: config.write(Wtf)