python中csv,xml,json读写
python csv读写
- 没有标题行的csv读写
import csv
fieldlist=[
['one', '1'],
['two', '2'],
['three', '3']
]
# 写入
with open("te_write.csv", 'w', newline='') as ff:
writer = csv.writer(ff, dialect="excel")
writer.writerows(fieldlist)
# 读取
with open("te_write.csv", 'r', newline='') as ff:
reader = csv.reader(ff)
content = [row for row in reader]
print(content)
- 带有标题行的csv读写
# 输出带有标题行
with open("te_writer.csv", 'r', newline='') as ff:
reader = csv.DictReader(ff, fieldnames=['first', 'last'])
content = [row for row in reader]
pprint(content)
# 具有标题行的写入
fieldlist_dict=[
{'first': 'one', 'last': '1'},
{'first': 'two', 'last': '2'},
{'first': 'three', 'last': '3'}
]
with open('te_writer.csv', 'w', newline='') as ff:
writer = csv.DictWriter(ff, ['first', 'last'])
writer.writeheader()
writer.writerows(fieldlist_dict)
# 具有标题行的csv读取
with open("te_writer.csv", 'r', newline='') as ff:
reader = csv.DictReader(ff, )
content = [row for row in reader]
pprint(content)
python xml读写
xml文件
<?xml version="1.0" encoding="utf-8" ?>
<menu>
<breakfast time="07:30">
<item price="50">tomato</item>
<item price="60">potato</item>
</breakfast>
<lunch time="11:30">
<item price="30">apple</item>
</lunch>
<dinner time="17:30">
<item price="50">banana</item>
</dinner>
</menu>
测试用例
import xml.etree.ElementTree as et
tree = et.ElementTree(file='test.xml')
root = tree.getroot()
for child in root:
print('tag:', child.tag, 'attributes:', child.attrib)
for grandchild in child:
print('\ttag:', grandchild.tag, 'attributes:', grandchild.attrib)
print(len(root)) # 3 menu下一共有3项
print(len(root[0])) # 2 menu中第一项有两项
输出
tag: breakfast attributes: {'time': '07:30'}
tag: item attributes: {'price': '50'}
tag: item attributes: {'price': '60'}
tag: lunch attributes: {'time': '11:30'}
tag: item attributes: {'price': '30'}
tag: dinner attributes: {'time': '17:30'}
tag: item attributes: {'price': '50'}
3
2
注意,xml库无法容纳10亿多的向,Defused XML列出了这种攻击和python库的其他缺点。可以避免数据过多或者使用defusedxml库解决数据项过多的问题
python读写json
json文件
{
"breakfast":{
"time":"07:30",
"items":{
"tomato":"50",
"potato":"40"
}
},
"lunch":{
"time":"11:30",
"items":{
"apple":"50"
}
},
"dinner":{
"time":"17:30",
"items":{
"banana":"17:30"
}
}
}
测试用例
dumps将字符串转为json对象
menu_json = json.dumps(menu)
print(menu_json)
# loads将json字符串转换为python数据对象
menu2 = json.loads(menu_json)
print(menu2)
输出
"{\n \"breakfast\":{\n \"time\":\"07:30\",\n \"items\":{\n \"tomato\":\"50\",\n \"potato\":\"40\"\n }\n },\n \"lunch\":{\n \"time\":\"11:30\",\n \"items\":{\n \"apple\":\"50\"\n }\n },\n \"dinner\":{\n \"time\":\"17:30\",\n \"items\":{\n \"banana\":\"17:30\"\n }\n }\n}"
{
"breakfast":{
"time":"07:30",
"items":{
"tomato":"50",
"potato":"40"
}
},
"lunch":{
"time":"11:30",
"items":{
"apple":"50"
}
},
"dinner":{
"time":"17:30",
"items":{
"banana":"17:30"
}
}
}
标准json中没有定义日期或时间类型,需要自定义处理方式,可以把datetime转换成json能够理解的类型,比如字符串或epoch
now = datetime.datetime.utcnow()
json.dumps(now)
抛出异常 TypeError: datetime.datetime(2016, 12, 15, 8, 50, 52, 633611) is not JSON serializable
可以通过继承修改json的编码方式,DTEncode是JSONEncoder的一个子类,可以重载它的default()方法来增加处理datetime的代码
import json
import datetime
from time import mktime
class DTEncoder(json.JSONEncoder):
def default(self, obj):
if isinstance(obj, datetime.datetime):
return int(mktime(obj.timetuple()))
return json.JSONEncoder.default(self, obj)
now = datetime.datetime.utcnow()
json.dumps(now, cls=DTEncoder)
输出
2016-12-15 08:54:31.071434
python配置文件读写
test.ini
[english]
greeting = hello
[french]
greeting = Bonjour
[files]
home = /usr/local
bin = %(home)s/bin
测试用例
import configparser
cfg = configparser.ConfigParser()
print(cfg.read("test.ini"))
print(cfg['french'])
print(cfg['files']['bin'])
输出
['test.ini']
<Section: french>
/usr/local/bin
python使用pickle序列化
import pickle
import datetime
now1 = datetime.datetime.utcnow()
pickled1 = pickle.dumps(now1)
print(now1)
print(pickled1)
now2 = pickle.loads(pickled1)
print(now2)
输出
2016-12-15 09:06:07.057386
b'\x80\x03cdatetime\ndatetime\nq\x00C\n\x07\xe0\x0c\x0f\t\x06\x07\x00\xe0*q\x01\x85q\x02Rq\x03.'
2016-12-15 09:06:07.057386