Python中CSV文件(逗號(hào)分割)實(shí)戰(zhàn)操作指南
一、csv文件介紹
1、csv文件簡(jiǎn)介
逗號(hào)分隔值(Comma-Separated Values,CSV,有時(shí)也稱(chēng)為字符分隔值,因?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)遍歷(從第二行讀取)
[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、寫(xiě)入文件(列表)
[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、寫(xiě)入文件(字典)
[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()
### 一行一行寫(xiě)入
for i in li:
writer.writerow(i)
### 直接把li寫(xiě)入(多行)
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讀寫(xiě)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)文章
python3.7中安裝paddleocr及paddlepaddle包的多種方法
這篇文章主要介紹了python3.7中安裝paddleocr及paddlepaddle包,本文通過(guò)多種方法給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-11-11
Python+OpenCV實(shí)現(xiàn)圖片及視頻中選定區(qū)域顏色識(shí)別
這篇文章主要為大家詳細(xì)介紹了如何利用Python+OpenCV實(shí)現(xiàn)圖片及視頻中選定區(qū)域顏色識(shí)別功能,文中的示例代碼講解詳細(xì),感興趣的可以了解一下2022-07-07
python使用XPath解析數(shù)據(jù)爬取起點(diǎn)小說(shuō)網(wǎng)數(shù)據(jù)
這篇文章主要介紹了python使用XPath解析數(shù)據(jù)爬取起點(diǎn)小說(shuō)網(wǎng)數(shù)據(jù),幫助大家更好的理解和學(xué)習(xí)使用python,感興趣的朋友可以了解下2021-04-04

