Python基础(5)- csv

这里简单介绍下Python中的csv模块,应该蛮常用的。
和csv有关,一定要回合打开文件这类操作有关,这里先看下这个open函数
官方文档:https://docs.python.org/3/library/functions.html#open

1.open

open(file, mode=’r’, buffering=-1, encoding=None, errors=None, newline=None, closefd=True, opener=None)Open file and return a corresponding file object. If the file cannot be opened, an OSError is raised.

file,就是我们要打开的文件地址;
mode,就是打开的方式,默认是只读文本(’rt’)

newline,和换行符有关,在网上找了个资料:https://www.zhihu.com/question/19751023

换行符看起来有点儿乱,以后如果一段问题了再研究下。
下面,我们先来看看csv

2. csv.reader

csv.reader(csvfile, dialect=’excel’, **fmtparams)Return a reader object which will iterate over lines in the given csvfile.

我们的csv文件是这样的

1
2
3
4
5
6
7
8
9
10
11
import csv
with open(r'D:\document\python_demo\employee_data.csv') as csvfile:
emp_reader = csv.reader(csvfile)
for row in emp_reader:
print(row)
##
runfile('D:/document/python_demo/demo_open.py', wdir='D:/document/python_demo')
['lufei', '20', 'leader', 'onepiece', '100']
['namei', '19', 'teacher', 'onepiece', '999']

就csv文件来说,会有几个特点,比如字段之间的分隔符,换行符等,我们使用上面的dialect来指定
如果我们,现在将分隔符,替换为^

我们再次执行,就无法正确分割数据了

1
2
3
runfile('D:/document/python_demo/demo_open.py', wdir='D:/document/python_demo')
['lufei^20^leader^onepiece^100']
['namei^19^teacher^onepiece^999']

我们修改下代码,加上delimiter就行了,详情参考官网:https://docs.python.org/3/library/csv.html#csv-fmt-params

1
2
3
4
5
6
7
8
9
10
11
import csv
with open(r'D:\document\python_demo\employee_data.csv') as csvfile:
emp_reader = csv.reader(csvfile,delimiter='^')
for row in emp_reader:
print(row)
##
runfile('D:/document/python_demo/demo_open.py', wdir='D:/document/python_demo')
['lufei', '20', 'leader', 'onepiece', '100']
['namei', '19', 'teacher', 'onepiece', '999']

这时候,如果我们的数据中,含有分隔符,我们需要再加上封闭符,一般都会使用双引号,这里使用参数quotechar指定,默认是双引号

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
csv data:
lufei^20^leader^$one^_^piece$^100
namei^19^teacher^onepiece^999
import csv
with open(r'D:\document\python_demo\employee_data.csv') as csvfile:
emp_reader = csv.reader(csvfile,delimiter='^',quotechar='$')
for row in emp_reader:
print(row)
result:
runfile('D:/document/python_demo/demo_open.py', wdir='D:/document/python_demo')
['lufei', '20', 'leader', 'one^_^piece', '100']
['namei', '19', 'teacher', 'onepiece', '999']

3.csv.writer

csv.writer(csvfile, dialect=’excel’, **fmtparams)Return a writer object responsible for converting the user’s data into delimited strings on the given file-like object. csvfile can be any object with a write() method.

这里的用法都差不多,我们简单举个小例子,用官网的例子

1
2
3
4
5
6
7
8
9
with open('eggs.csv', 'w', newline='') as csvfile:
spamwriter = csv.writer(csvfile, delimiter=' ',
quotechar='|', quoting=csv.QUOTE_MINIMAL)
spamwriter.writerow(['Spam'] * 5 + ['Baked Beans'])
spamwriter.writerow(['Spam', 'Lovely |Spam', 'Wonderful Spam'])
result:
|Spam| |Spam| |Spam| |Spam| |Spam| |Baked Beans|
|Spam| |Lovely ||Spam| |Wonderful Spam|

这里用到了另一个参数:quoting,这个参数是针对quotechar来说的,
quotechar在ETL工具中叫做封闭符,是为了防止字段内容中出现分割符,我们需要区分到底是分隔符,还是字段内容,所以需要根据quotechar去判断;
quoting则是控制在什么情况下使用封闭符,他有几个选项

1
2
3
4
csv.QUOTE_ALL #所有字段都添加封闭符
csv.QUOTE_NONNUMERIC #在非数值字段加封闭符
csv.QUOTE_NONE #所有字段都不加
csv.QUOTE_MINIMAL #只在出现分隔符的字段旁加封闭符,默认

好了,csv的就简单分享到这里了。

于贵洋 wechat
要教我弹吉他嘛!