Python中用于序列化的两个模块
json 用于『字符串』和『python基本数据类型』间进行转换pickle 用于『python特有的类型』和『python基本数据类型』间进行转换json模块提供了四个功能:dumps、dump、loads、load
pickle模块提供了四个功能:dumps、dump、loads、load
例子:json:
#!/usr/bin/env python3 import json user_info = {'name':'tom','age':18} user_str = json.dumps(dic) print(user_str,type(user_str)) data = json.loads(s) print(data,type(data)) with open('user.txt', 'w') as f: json.dump(user_info,f) with open('user.txt', 'r') as f: user_info = json.load(f) print(user_info,type(user_info)) 结果: {"age": 18, "name": "tom"} <class 'str'> {'age': 18, 'name': 'tom'} <class 'dict'> {'age': 18, 'name': 'tom'} <class 'dict'>查看文件内容:
{root@localhost ~} # cat user.txt {"age": 18, "name": "tom"}pickle:
#!/usr/bin/env python3 import pickle user_info = {'name':'tom','age':18} user_str = pickle.dumps(user_info) print(user_str,type(user_str)) data = pickle.loads(user_str) print(data,type(data)) with open('user.txt', 'wb') as f: pickle.dump(user_info,f) with open('user.txt', 'rb') as f: user_info = pickle.load(f) print(user_info,type(user_info)) 结果: b'\x80\x03}q\x00(X\x03\x00\x00\x00ageq\x01K\x12X\x04\x00\x00\x00nameq\x02X\x03\x00\x00\x00tomq\x03u.' <class 'bytes'> {'age': 18, 'name': 'tom'} <class 'dict'> {'age': 18, 'name': 'tom'} <class 'dict'>查看文件内容:
[root@localhost ~]# cat user.txt (XageqKXnameqXtomqu.Python的字符串格式化有两种方式: 百分号方式、format方式 百分号的方式相对来说比较老,而format方式则是比较先进的方式,企图替换古老的方式,目前两者并存。[PEP-3101]
常用的格式化例子:
>>> s = "I'am %s" % 'tom' >>> print(s) I'am tom >>> s = "I'am %s,%d years old!" % ('tom',18) >>> print(s) I'am tom,18 years old! >>> s = "I'm %(name)s, %(age)d years old!" % {'name':'jerry','age':20} >>> print(s) I'm jerry, 20 years old! >>> s = "percent %.2f" % 99.97643 >>> print(s) percent 99.98 >>> s = "I have %(percent).2f confidence" % {'percent':99.991} >>> print(s) I have 99.99 confidence >>> s = "I have %(percent).2f%% confidence" % {'percent':99.991} >>> print(s) I have 99.99% confidence常用的format方式格式化的例子:
>>> s = "I am {},{} years old,{},how about you?".format('jack', 18, 'rose') >>> print(s) I am jack,18 years old,rose,how about you? >>> s = "i am {}, age {}, {}, how about you?".format(*['jack', 18, 'rose']) >>> print(s) i am jack, age 18, rose, how about you? >>> s = "I am {0}, {1} years old, really {0}".format('tom', 18) >>> print(s) I am tom, 18 years old, really tom >>> s = "I am {0}, {1} years old, really {0}".format(*['tom',18]) >>> print(s) I am tom, 18 years old, really tom >>> s = "I am {name}, {age} years old, really {name}".format(name='tom', age=18) >>> print(s) I am tom, 18 years old, really tom >>> s = "I am {name}, {age} years old, really {name}".format(**{'name':'tom','age':18}) >>> print(s) I am tom, 18 years old, really tom >>> s = "{0[0]} apple, {0[1]} bananas, {1[0]} oranges".format([1, 2, 3], [11, 22, 33]) >>> print(s) 1 apple, 2 bananas, 11 oranges >>> s = "I am {:s}, {:d} years old, have {:f} dollars".format('jack', 18, 88888.1) >>> print(s) I am jack, 18 years old, have 88888.100000 dollars >>> s = "I am {:s}, {:d} years old!".format(*['tom', 18]) >>> print(s) I am tom, 18 years old! >>> s = "I am {name:s}, {age:d} years old!".format(name='tom', age=18) >>> print(s) I am tom, 18 years old! >>> s = "I am {name:s}, {age:d} years old!".format(**{'name':'tom', 'age':18}) >>> print(s) I am tom, 18 years old! >>> s = "numbers: {:b},{:o},{:d},{:x},{:X}, {:%}".format(15, 15, 15, 15, 15, 15.87623, 2) >>> print(s) numbers: 1111,17,15,f,F, 1587.623000% >>> s = "numbers: {0:b},{0:o},{0:d},{0:x},{0:X}, {0:%}".format(15) >>> print(s) numbers: 1111,17,15,f,F, 1500.000000% >>> s = "numbers: {num:b},{num:o},{num:d},{num:x},{num:X}, {num:%}".format(num=15) >>> print(s) numbers: 1111,17,15,f,F, 1500.000000%转载于:https://www.cnblogs.com/zhangxunan/p/5567032.html