python读写csv_xml_json配置文件

本文详细介绍了Python中如何进行csv,xml和json文件的读写操作,包括无标题行和带标题行的csv处理,XML文件读写时的数据量限制问题,以及JSON文件中日期类型的处理。此外,还提到了Python使用pickle进行序列化的应用场景。" 38819173,4238633,使用JWPL构建中文维基百科数据库,"['数据库构建', '数据处理', 'JWPL工具', '维基百科API', '信息检索']
摘要由CSDN通过智能技术生成

python中csv,xml,json读写

python csv读写

  1. 没有标题行的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)
  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 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
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值