Python中CSV文件(逗號(hào)分割)實(shí)戰(zhàn)操作指南
一、csv文件介紹
1、csv文件簡(jiǎn)介
逗號(hào)分隔值(Comma-Separated Values,CSV,有時(shí)也稱為字符分隔值,因?yàn)榉指糇址部梢圆皇嵌禾?hào)),其文件以純文本形式存儲(chǔ)表格數(shù)據(jù)(數(shù)字和文本)。純文本意味著該文件是一個(gè)字符序列,不含必須像二進(jìn)制數(shù)字那樣被解讀的數(shù)據(jù)。CSV文件由任意數(shù)目的記錄組成,記錄間以某種換行符分隔;每條記錄由字段組成,字段間的分隔符是其它字符或字符串,最常見(jiàn)的是逗號(hào)或制表符。通常,所有記錄都有完全相同的字段序列。通常都是純文本文件。
2、為什么要使用csv文件
在Linux中我們可以通過(guò)命令在數(shù)據(jù)庫(kù)中把表導(dǎo)出來(lái)為csv結(jié)尾的文件,其實(shí)就是以逗號(hào)分割分txt文件,此文件我們可以在windows中打開(kāi)并且為表格的形式,方便我們進(jìn)行查看與再次操作。
eg:
MariaDB [test]> select * from 表名 into outfile "/tmp/test.csv" fields terminated by ",";
二、csv文件查看
注意:這里我是把csv文件和python代碼都放在同級(jí)目錄,否則要指定路徑!??!
1、測(cè)試文件創(chuàng)建
(1)這里我們以windows中的csv文件來(lái)做實(shí)驗(yàn)
(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 ### 把空格替換為逗號(hào) [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) ### 這個(gè)就是我們得表頭 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)點(diǎn):我們不知道表頭在具體那列,我們可以通過(guò)表頭名來(lái)獲取整列數(shù)據(jù),即我們可以隨便調(diào)整順序也不會(huì)影響我們的數(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文件(逗號(hào)分割)的文章就介紹到這了,更多相關(guān)Python CSV文件逗號(hào)分割內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Python操作csv文件之csv.writer()和csv.DictWriter()方法的基本使用
- Python Pandas讀寫txt和csv文件的方法詳解
- Python 修改CSV文件實(shí)例詳解
- python讀取和保存為excel、csv、txt文件及對(duì)DataFrame文件的基本操作指南
- 利用python合并csv文件的方式實(shí)例
- Python讀取CSV文件并進(jìn)行數(shù)據(jù)可視化繪圖
- python用pd.read_csv()方法來(lái)讀取csv文件的實(shí)現(xiàn)
- Python如何讀取csv文件時(shí)添加表頭/列名
- Python 比較兩個(gè) CSV 文件的三種方法并打印出差異
相關(guān)文章
Python超簡(jiǎn)單容易上手的畫圖工具庫(kù)推薦
今天小編給大家分享一款很棒的python畫圖工具庫(kù),本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2021-05-05python pandas輕松通過(guò)特定列的值多條件去篩選數(shù)據(jù)及contains方法的使用
這篇文章主要介紹了python pandas輕松通過(guò)特定列的值多條件去篩選數(shù)據(jù)及contains方法的使用,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-02-02python同時(shí)給兩個(gè)收件人發(fā)送郵件的方法
這篇文章主要介紹了python同時(shí)給兩個(gè)收件人發(fā)送郵件的方法,涉及Python使用smtplib包發(fā)送郵件的相關(guān)技巧,需要的朋友可以參考下2015-04-04Python使用multiprocessing模塊實(shí)現(xiàn)多進(jìn)程并發(fā)處理大數(shù)據(jù)量的示例代碼
這篇文章主要介紹了Python使用multiprocessing模塊實(shí)現(xiàn)多進(jìn)程并發(fā)處理大數(shù)據(jù)量的示例代碼,本文通過(guò)示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2024-01-01pytest接口測(cè)試之fixture傳參數(shù)request的使用
本文主要介紹了pytest接口測(cè)試之fixture傳參數(shù)request的使用,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-08-08基于python,Matplotlib繪制函數(shù)的等高線與三維圖像
這篇文章主要介紹了基于python,Matplotlib繪制函數(shù)的等高線與三維圖像,函數(shù)的等高線及其三維圖像的可視化方法,下面一起來(lái)學(xué)習(xí)具體內(nèi)容吧,需要的小伙伴可以參考一下2022-01-01