這里簡單介紹下Python中的csv模塊夯巷,應(yīng)該蠻常用的。
和csv有關(guān),一定要回合打開文件這類操作有關(guān)掀鹅,這里先看下這個open函數(shù)
官方文檔: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劫灶,和換行符有關(guān),在網(wǎng)上找了個資料: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文件是這樣的
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來指定
如果我們蒲犬,現(xiàn)在將分隔符,替換為^
我們再次執(zhí)行岸啡,就無法正確分割數(shù)據(jù)了
runfile('D:/document/python_demo/demo_open.py', wdir='D:/document/python_demo')
['lufei^20^leader^onepiece^100']
['namei^19^teacher^onepiece^999']
我們修改下代碼原叮,加上delimiter就行了,詳情參考官網(wǎng):https://docs.python.org/3/library/csv.html#csv-fmt-params
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']
這時候巡蘸,如果我們的數(shù)據(jù)中奋隶,含有分隔符,我們需要再加上封閉符悦荒,一般都會使用雙引號唯欣,這里使用參數(shù)quotechar指定,默認是雙引號
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.
這里的用法都差不多搬味,我們簡單舉個小例子境氢,用官網(wǎng)的例子
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|
這里用到了另一個參數(shù):quoting蟀拷,這個參數(shù)是針對quotechar來說的,
quotechar在ETL工具中叫做封閉符萍聊,是為了防止字段內(nèi)容中出現(xiàn)分割符问芬,我們需要區(qū)分到底是分隔符,還是字段內(nèi)容寿桨,所以需要根據(jù)quotechar去判斷此衅;
quoting則是控制在什么情況下使用封閉符,他有幾個選項
csv.QUOTE_ALL #所有字段都添加封閉符
csv.QUOTE_NONNUMERIC #在非數(shù)值字段加封閉符
csv.QUOTE_NONE #所有字段都不加
csv.QUOTE_MINIMAL #只在出現(xiàn)分隔符的字段旁加封閉符亭螟,默認
好了挡鞍,csv的就簡單分享到這里了。