一、读写CSV数据

(1)使用csv库处理CSV数据

import csv
with open('./stock.csv') as f:
    f_csv = csv.reader(f)
    headers = next(f_csv)
    for row in f_csv:
        # process row

由于每一行的row是个列表,访问需要用row[0]、row[1],

(2)可以考虑转换成命名元组访问。

'''
遇到问题没人解答?小编创建了一个Python学习交流QQ群:579817333 
寻找有志同道合的小伙伴,互帮互助,群里还有不错的视频学习教程和PDF电子书!
'''
import csv
from collections import namedtuple

with open('./stock.csv') as f:
    f_csv = csv.reader(f)
    headers = next(f_csv)
    Row = namedtuple('Row',headers)
    for r in f_csv:
        row = Row(*r)
        # process row

(3)转换为字典

import csv
with open('./stock.csv') as f:
    f_csv = csv.DictReader(f)
    for row in f_csv:
        # process row

写入CSV数据:

'''
遇到问题没人解答?小编创建了一个Python学习交流QQ群:579817333 
寻找有志同道合的小伙伴,互帮互助,群里还有不错的视频学习教程和PDF电子书!
'''
import csv
headers = ['Symbol', 'Price', 'Date', 'Time', 'Change', 'Volume']
rows = [
    ('AA', '39.48', '6/11/2007', '9:34am', '-0.18', '428900'),
    ('BB', '48.54', '8/25/2001', '19:57am', '-0.44', '142800'),
    ('CC', '92.13', '3/18/1886', '3:11am', '-0.67', '126700'),
    ('DD', '79.25', '2/05/1999', '8:22am', '-0.27', '110000'),
]

with open('stock2.csv','w') as f:
    f_csv = csv.writer(f)
    f_csv.writerow(headers)
    f_csv.writerows(rows)

如果数据是字典序列,那么可以这样处理:

import csv
headers = ['Symbol', 'Price', 'Date', 'Time', 'Change', 'Volume']
rows = [
    {'Symbol':'AA','Price':39.48,'Date':'6/11/2007', 'Time':'9:34am', 'Change':-0.18, 'Volume':428900}
]

with open('stock2.csv','w') as f:
    f_csv = csv.DictWriter(f, headers)
    f_csv.writeheader()
    f_csv.writerows(rows)

标题行出现非法字符,需要进行转换。

import re
with open('./stock.csv') as f:
    f_csv = csv.reader(f)
    headers = [ re.sub('[^a-zA-Z_]', '_', h) for h in next(f_csv)]

读取数据时,将部分数据转换成除字符串之外的类型。

'''
遇到问题没人解答?小编创建了一个Python学习交流QQ群:579817333 
寻找有志同道合的小伙伴,互帮互助,群里还有不错的视频学习教程和PDF电子书!
'''
import csv,re

col_type = [str,float,str,str,float,str]
with open('./stock.csv') as f:
    f_csv = csv.reader(f)
    headers = [ re.sub('[^a-zA-Z_]', '_', h) for h in next(f_csv)]
    for row in f_csv:
        row = tuple( convert(value)for convert, value in zip(col_type, row) )

字段转化成字典:

field_type = [
    ('Price',float),
    ('Change',float),
    ('Volume',int),
]

with open('./stock.csv') as f:
    for row in csv.DictReader(f):
        row.update( (key,convert(row[key])) for key, convert in field_type)
        print(row)

二、读写JSON数据

(1)字符串形式:json.dumps()、json.loads()

(2)文件形式:json.dump()、json.load()

(3)使用pprint()函数,合理格式输出 或者 在json.dumps()函数中使用indext参数

'''
遇到问题没人解答?小编创建了一个Python学习交流QQ群:579817333 
寻找有志同道合的小伙伴,互帮互助,群里还有不错的视频学习教程和PDF电子书!
'''
>>> from urllib.request import urlopen

>>> pprint(json_resp)

>>> print(json.dumps(data, indent=4))

(4)load时解码为OrderDict有序字典

>>> from collections import OrderedDict

>>> data = json.loads(s, object_pairs_hook=OrderedDict)

(5)JSON字典转变为Python对象