Python处理常⽤结构化⽂本⽂件《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 = ader(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⽂件
ElementTree as et
tree = et.ElementTree(file='l')
root = t()
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()
print(cfg['french']['greeting']) # Bonjour
print(cfg['files']['bin']) # /usr/local/bin
复制代码
5. 使⽤pickle序列化
python处理xml文件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
复制代码
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论