《Python语言及其应用》的学习笔记
1. CSV
写入CSV
文件
- import csv
-
- alphabet = [
- ('Char', 'No'),
- ('a', 1),
- ('b', 2),
- ('c', 3),
- ]
-
- # 如果写入的文件出现多个空行,则在打开文件时,设置newline为空(newline='')
- with open('alphabet.csv', 'wt', encoding='utf-8', newline='') as fout:
- csvout = csv.writer(fout)
- csvout.writerows(alphabet)
- 复制代码
读取CSV
文件
- with open('alphabet.csv', 'rt', encoding='utf-8') as fin:
- cin = csv.reader(fin)
- alphabet = [row for row in cin]
-
- print(alphabet) # [['Char', 'No'], ['a', '1'], ['b', '2'], ['c', '3']]
- 复制代码
使用DictWriter
和DictReader
- import csv
-
- alphabet = [
- {'Char': 'a', 'No': 1},
- {'Char': 'b', 'No': 2},
- {'Char': 'c', 'No': 3}
- ]
-
- with open('alphabet.csv', 'wt', encoding='utf-8', newline='') as fout:
- csvout = csv.DictWriter(fout, ['Char', 'No'])
- csvout.writeheader()
- csvout.writerows(alphabet)
-
- with open('alphabet.csv', 'rt', encoding='utf-8') as fin:
- cin = csv.DictReader(fin)
- alphabet = [dict(row) for row in cin]
-
- print(alphabet) # [{'Char': 'a', 'No': '1'}, {'Char': 'b', 'No': '2'}, {'Char': 'c', 'No': '3'}]
- 复制代码
2. XML
通常用于数据传送和消息, 如RSS和Atom
- <?xml version="1.0" encoding="utf-8" ?>
- <menu>
- <breakfast hours="7-11">
- <item price="$6.00">breakfast burritos</item>
- <item price="$4.00">pancakes</item>
- </breakfast>
- <lunch hours="11-3">
- <item price="$5.00">hamburger</item>
- </lunch>
- <dinner hours="3-10">
- <item price="8.00">spaghetti</item>
- </dinner>
- </menu>
- 复制代码
使用ElementTree
解析xml
文件
- import xml.etree.ElementTree as et
-
- tree = et.ElementTree(file='menu.xml')
- root = tree.getroot()
-
- print(root.tag) # menu
-
- for child in root:
- print(child.tag, child.attrib)
- for grandchild in child:
- print('\t', grandchild.tag, grandchild.attrib)
- # breakfast {'hours': '7-11'}
- # item {'price': '$6.00'}
- # item {'price': '$4.00'}
- # lunch {'hours': '11-3'}
- # item {'price': '$5.00'}
- # dinner {'hours': '3-10'}
- # item {'price': '8.00'}
-
- print(len(root)) # 3
-
- print(len(root[0])) # 2
- 复制代码
3. JSON
- 使用
json.dumps()
编码成JSON
字符串 - 使用
json.loads()
将JSON
字符串解析成Python数据结构
- import json
- from datetime import datetime
-
- now = datetime.utcnow()
- json.dumps(now)
- # 报错TypeError: Object of type 'datetime' is not JSON serializable
-
- # 需要将datetime转换成JSON能够理解的类型,如str或epoch
- now_str = str(now)
- json.dumps(now_str)
- 复制代码
通过继承和修改JSON的编码方式,使其支持datetime
- import json
- from datetime import datetime
- from time import mktime
-
- now = datetime.utcnow()
-
- class DTEncoder(json.JSONEncoder):
- def default(self, o):
- if isinstance(o, datetime):
- return int(mktime(o.timetuple()))
- return json.JSONEncoder.default(self, o)
-
- json.dumps(now, cls=DTEncoder)
- 复制代码
4. 配置文件
settings.cfg
配置文件
- [english]
- greeting = Hello
-
- [french]
- greeting = Bonjour
-
- [files]
- home = /usr/local
-
- # 简单的插入
- bin = %(home)s/bin
- 复制代码
使用configparser
读取配置文件
- import configparser
- cfg = configparser.ConfigParser()
- cfg.read('settings.cfg', encoding='utf-8')
-
- print(cfg['french']['greeting']) # Bonjour
- print(cfg['files']['bin']) # /usr/local/bin
- 复制代码
5. 使用pickle
序列化
pickle
是以特殊的二进制格式保存和恢复数据的
- import pickle
- from datetime import datetime
-
- now = datetime.utcnow()
-
- now_pickled = pickle.dumps(now) # b'\x80\x03cdatetime\ndatetime\nq\x00C\n\x07\xe2\x03\x10\x034)\x00\xca_q\x01\x85q\x02Rq\x03.'
- now2 = pickle.loads(now_pickled) # 2018-03-16 03:52:41.051807
- 复制代码