日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > python >内容正文

python

python处理csv文件案例_python3读取csv文件任意行列代码实例

發(fā)布時(shí)間:2025/4/5 python 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python处理csv文件案例_python3读取csv文件任意行列代码实例 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

讀取每一行

reader = csv.reader(f) 此時(shí)reader返回的值是csv文件中每行的列表,將每行讀取的值作為列表返回

#讀取每一行

filename='D:\\file_information1.csv'

import csv

with open(filename,newline = '',encoding = 'utf-8') as f: #參數(shù)encoding =

'utf-8'防止出現(xiàn)亂碼

reader = csv.reader(f) #使用csv的reader()方法,創(chuàng)建一個(gè)reader對(duì)象

csv.reader()讀取結(jié)果是列表

for row in reader: #遍歷reader對(duì)象的每一行

print(row)

如何往csv格式文件寫(xiě)入數(shù)據(jù)

1.write()函數(shù)寫(xiě)入文本文件的也是字符串類型。

2.在’w’和’a’模式下,如果你打開(kāi)的文件不存在,那么open()函數(shù)會(huì)自動(dòng)幫你創(chuàng)建一個(gè)

3.'w’寫(xiě)入模式會(huì)給你強(qiáng)行清空掉文件,然后再給你寫(xiě)入。如果你只想增加?xùn)|西,而不想完全覆蓋掉原文件的話,就要使用’a’模式,表示append,你學(xué)過(guò),它是追加的意思。

file1 = open('D:\\new\\abc.txt','a',encoding='utf-8')

file1.write('張無(wú)忌\n')

file1.write('宋青書(shū)\n')

file1.close()

enumerate()

enumerate() 函數(shù)用于將一個(gè)可遍歷的數(shù)據(jù)對(duì)象(如列表、元組或字符串)組合為一個(gè)索引序列,同時(shí)列出數(shù)據(jù)和數(shù)據(jù)下標(biāo) ,一般用在 for

循環(huán)當(dāng)

以下是 enumerate() 方法的語(yǔ)法: enumerate(sequence, [start=0])

sequence – 一個(gè)序列、迭代器或其他支持迭代對(duì)象。

start – 下標(biāo)起始位置。

返回 enumerate(枚舉) 對(duì)象

seasons = ['Spring', 'Summer', 'Fall', 'Winter']

print(list(enumerate(seasons))) # [(0, 'Spring'), (1, 'Summer'), (2,

'Fall'), (3, 'Winter')]

print(list(enumerate(seasons, start=1)) ) # [(1, 'Spring'), (2, 'Summer'),

(3, 'Fall'), (4, 'Winter')]

# 普通for循環(huán):

i = 0

seq = ['one', 'two', 'three']

for element in seq:

print( i, seq[i])

i +=1

'''

one

two

three

'''

# for 循環(huán)使用 enumerate

seq1 = ['one', 'two', 'three']

for j, element in enumerate(seq1):

print (j, element)

'''

one

two

three

'''

獲取文件的編碼方式

import chardet

def get_file_code(file_path):

with open(file_path, 'rb') as f:

data = f.read()

print('獲取到的CSV文件編碼為:%s' % (chardet.detect(data)['encoding']))

return chardet.detect(data)['encoding']

file_path = r'D:\\file_information1.csv'

get_file_code(file_path)

判斷文件的編碼方式

f = open("D:\\file_information1.csv","rb")#二進(jìn)制格式讀文件

i = 0

while True:

print(i)

line = f.readline()

if not line:

break

else:

try:

# print(line)

# print(line.decode('utf8'))

line.decode('utf8')

#為了暴露出錯(cuò)誤,最好此處不print

except:

print(str(line))

i += 1

讀取首行

filename='D:\\file_information1.csv'

with open(filename,'r',encoding='utf-8')as f: #自行先判斷文件的編碼方式

read=f.readlines()

for index,info in enumerate(read):

if index ==0: #這里判斷

#這里輸出的是字符串類型

print(info)

讀取首行之外的所有行

filename='D:\\file_information1.csv'

with open(filename,'r',encoding='utf-8')as f:

read=f.readlines()

for index,info in enumerate(read):

if index !=0: #這里判斷

print(info)

讀取前10行鄭州婦科醫(yī)院 http://m.zyfuke.com/

filename='D:\\file_information1.csv'

with open(filename,'r',encoding='utf-8')as f:

read=f.readlines()

for index,info in enumerate(read):

if index <10: #這里為索引,是 int 轉(zhuǎn)型

print(index,info)

讀取任意行,可根據(jù)index索引

import csv

filename='D:\\file_information1.csv'

with open(filename,'r',encoding='utf-8')as f:

read=f.readlines()

for index,info in enumerate(read):

print(index) #自己根據(jù)index的數(shù)字判斷

讀取第一和第二列

import csv

filename='D:\\file_information1.csv'

with open(filename,'r',encoding='utf-8')as f:

read=csv.reader(f)

for index,info in enumerate(read):

#這里輸出的是列表類型

print(info[:2]) #[:2]代表的是讀取第0列和第1列 ,第2列不包括

讀取除首行之外的第一,第二列

import csv

filename='D:\\file_information1.csv'

with open(filename,'r',encoding='utf-8')as f:

read=csv.reader(f)

for index,info in enumerate(read):

if index!=0: #這里加判斷

print(info[:2])

讀取最后兩列

import csv

filename='D:\\file_information1.csv'

with open(filename,'r',encoding='utf-8')as f:

read=csv.reader(f)

for index,info in enumerate(read):

print(info[-2:])

讀第一行的第三列

filename='D:\\file_information1.csv'

with open(filename,'r',encoding='utf-8')as f:

read=csv.reader(f)

for index,info in enumerate(read):

if index==0:

print(info[2:3])

總結(jié)

以上是生活随笔為你收集整理的python处理csv文件案例_python3读取csv文件任意行列代码实例的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。