Python中CSV文件(逗號分割)實戰(zhàn)操作指南
一、csv文件介紹
1、csv文件簡介
逗號分隔值(Comma-Separated Values,CSV,有時也稱為字符分隔值,因為分隔字符也可以不是逗號),其文件以純文本形式存儲表格數(shù)據(jù)(數(shù)字和文本)。純文本意味著該文件是一個字符序列,不含必須像二進(jìn)制數(shù)字那樣被解讀的數(shù)據(jù)。CSV文件由任意數(shù)目的記錄組成,記錄間以某種換行符分隔;每條記錄由字段組成,字段間的分隔符是其它字符或字符串,最常見的是逗號或制表符。通常,所有記錄都有完全相同的字段序列。通常都是純文本文件。
2、為什么要使用csv文件
在Linux中我們可以通過命令在數(shù)據(jù)庫中把表導(dǎo)出來為csv結(jié)尾的文件,其實就是以逗號分割分txt文件,此文件我們可以在windows中打開并且為表格的形式,方便我們進(jìn)行查看與再次操作。
eg:
MariaDB [test]> select * from 表名 into outfile "/tmp/test.csv" fields terminated by ",";
二、csv文件查看
注意:這里我是把csv文件和python代碼都放在同級目錄,否則要指定路徑?。?!
1、測試文件創(chuàng)建
(1)這里我們以windows中的csv文件來做實驗
(2)我們可以選中內(nèi)容復(fù)制進(jìn)去,也可以上傳到linux中,這里我們選擇前者
[root@python _test]# vim test.csv ###可以發(fā)現(xiàn)復(fù)制進(jìn)去的是以空格為分隔符 id username passwd age 1 dream1 123 21 2 dream2 456 22 3 dream3 789 23 ### 把空格替換為逗號 [root@python _test]# sed -i 's/\s\+/,/g' test.csv
2、查看csv文件(列表)
(1)讀出結(jié)果
[root@python _test]# vim _test.py #!/usr/bin/env python #coding:utf-8 import csv with open('test.csv', encoding="utf-8") as f: reader = csv.reader(f) print(reader) print(list(reader)) ### 查看結(jié)果 [root@python _test]# python _test.py <_csv.reader object at 0x7f54d9a01eb8> [['id', 'username', 'passwd', 'age'], ['1', 'dream1', '123', '21'], ['2', 'dream2', '456', '22'], ['3', 'dream3', '789', '23']]
(2)遍歷(從第一行讀?。?/p>
[root@python _test]# vim _test.py #!/usr/bin/env python #coding:utf-8 import csv with open('test.csv', encoding="utf-8") as f: reader = csv.reader(f) for i in reader: print(reader.line_num, i) ### 查看結(jié)果 [root@python _test]# python _test.py 1 ['id', 'username', 'passwd', 'age'] 2 ['1', 'dream1', '123', '21'] 3 ['2', 'dream2', '456', '22'] 4 ['3', 'dream3', '789', '23']
(2)遍歷(從第二行讀?。?/p>
[root@python _test]# vim _test.py #!/usr/bin/env python #coding:utf-8 import csv with open('test.csv', encoding="utf-8") as f: reader = csv.reader(f) ### 這個就是我們得表頭 next(reader) for i in reader: print(reader.line_num, i) ### 查看結(jié)果 [root@python _test]# python _test.py 2 ['1', 'dream1', '123', '21'] 3 ['2', 'dream2', '456', '22'] 4 ['3', 'dream3', '789', '23']
3、查看csv文件(字典)
(1)查看
[root@python _test]# vim _test.py #!/usr/bin/env python #coding:utf-8 import csv with open('test.csv', encoding="utf-8") as f: reader = csv.DictReader(f) ### 表頭 print (reader.fieldnames) print (reader,type(reader)) for i in reader: print (i) ### 查看結(jié)果 [root@python _test]# python _test.py ['id', 'username', 'passwd', 'age'] <csv.DictReader object at 0x7f3b02213a20> <class 'csv.DictReader'> OrderedDict([('id', '1'), ('username', 'dream1'), ('passwd', '123'), ('age', '21')]) OrderedDict([('id', '2'), ('username', 'dream2'), ('passwd', '456'), ('age', '22')]) OrderedDict([('id', '3'), ('username', 'dream3'), ('passwd', '789'), ('age', '23')])
(2)查看第一列(id)
優(yōu)點:我們不知道表頭在具體那列,我們可以通過表頭名來獲取整列數(shù)據(jù),即我們可以隨便調(diào)整順序也不會影響我們的數(shù)據(jù)讀取?。?!
[root@python _test]# vim _test.py #!/usr/bin/env python #coding:utf-8 import csv with open('test.csv', encoding="utf-8") as f: reader = csv.DictReader(f) for i in reader: print (i['id']) ### 查看結(jié)果 [root@python _test]# python _test.py 1 2 3
4、寫入文件(列表)
[root@python _test]# vim _test.py #!/usr/bin/env python #coding:utf-8 import csv li = [["id","user","性別"],["1","dreamya1","男"],["2","dreamya2","女"]] with open('user.csv', 'w', newline='') as f: writer = csv.writer(f) for i in li: writer.writerow(i) ### 查看結(jié)果 [root@python _test]# python _test.py [root@python _test]# cat user.csv id,user,性別 1,dreamya1,男 2,dreamya2,女
下載到windows中查看:
[root@python _test]# sz user.csv
5、寫入文件(字典)
[root@python _test]# vim _test.py import csv #coding:utf-8 headers = ['id', 'username','passwd'] li = [{'id':'1','username':'dream1','passwd':'123'}, {'id':'2','username':'dream2','passwd':'456'}, ] with open('user.csv', 'w', newline='') as f: ### 表頭傳入 writer = csv.DictWriter(f, headers) writer.writeheader() ### 一行一行寫入 for i in li: writer.writerow(i) ### 直接把li寫入(多行) writer.writerows(li) ### 查看結(jié)果 [root@python _test]# python _test.py [root@python _test]# cat user.csv id,username,passwd 1,dream1,123 2,dream2,456 1,dream1,123 2,dream2,456
windows中查看:
[root@python _test]# sz user.csv
總結(jié)
到此這篇關(guān)于Python中CSV文件(逗號分割)的文章就介紹到這了,更多相關(guān)Python CSV文件逗號分割內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python pandas輕松通過特定列的值多條件去篩選數(shù)據(jù)及contains方法的使用
這篇文章主要介紹了python pandas輕松通過特定列的值多條件去篩選數(shù)據(jù)及contains方法的使用,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-02-02Python使用multiprocessing模塊實現(xiàn)多進(jìn)程并發(fā)處理大數(shù)據(jù)量的示例代碼
這篇文章主要介紹了Python使用multiprocessing模塊實現(xiàn)多進(jìn)程并發(fā)處理大數(shù)據(jù)量的示例代碼,本文通過示例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友參考下吧2024-01-01pytest接口測試之fixture傳參數(shù)request的使用
本文主要介紹了pytest接口測試之fixture傳參數(shù)request的使用,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-08-08基于python,Matplotlib繪制函數(shù)的等高線與三維圖像
這篇文章主要介紹了基于python,Matplotlib繪制函數(shù)的等高線與三維圖像,函數(shù)的等高線及其三維圖像的可視化方法,下面一起來學(xué)習(xí)具體內(nèi)容吧,需要的小伙伴可以參考一下2022-01-01