python读写csv_xml_json配置文件

来源:互联网 发布:linux启动盘制作 编辑:程序博客网 时间:2024/05/20 18:17

python中csv,xml,json读写

python csv读写

  1. 没有标题行的csv读写
import csvfieldlist=[    ['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)
  1. 带有标题行的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 ettree = 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'}32

注意,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 jsonimport datetimefrom time import mktimeclass 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/localbin = %(home)s/bin

测试用例

import configparsercfg = configparser.ConfigParser()print(cfg.read("test.ini"))print(cfg['french'])print(cfg['files']['bin'])输出['test.ini']<Section: french>/usr/local/bin

python使用pickle序列化

import pickleimport datetimenow1 = datetime.datetime.utcnow()pickled1 = pickle.dumps(now1)print(now1)print(pickled1)now2 = pickle.loads(pickled1)print(now2)输出2016-12-15 09:06:07.057386b'\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
0 0
原创粉丝点击