python怎么读取csv文件-python3读取csv文件任意行列代码实例

这篇文章主要介绍了python3读取csv文件任意行列代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

读取每一行

reader = csv.reader(f) 此时reader返回的值是csv文件中每行的列表,将每行读取的值作为列表返回

?

1

2

3

4

5

6

7

8

#读取每一行

filename='D:\file_information1.csv'

import csv

withopen(filename,newline= '',encoding = 'utf-8') as f: #参数encoding = 'utf-8'防止出现乱码

reader= csv.reader(f)#使用csv的reader()方法,创建一个reader对象 csv.reader()读取结果是列表

for rowin reader:#遍历reader对象的每一行

print(row)

如何往csv格式文件写入数据

1.write()函数写入文本文件的也是字符串类型。

2.在'w'和'a'模式下,如果你打开的文件不存在,那么open()函数会自动帮你创建一个

3.'w'写入模式会给你暴力清空掉文件,然后再给你写入。如果你只想增加东西,而不想完全覆盖掉原文件的话,就要使用'a'模式,表示append,你学过,它是追加的意思。

?

1

2

3

4

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

file1.write('张无忌 ')

file1.write('宋青书 ')

file1.close()

enumerate()

enumerate() 函数用于将一个可遍历的数据对象(如列表、元组或字符串)组合为一个索引序列,同时列出数据和数据下标 ,一般用在 for 循环当

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

sequence -- 一个序列、迭代器或其他支持迭代对象。

start -- 下标起始位置。

返回 enumerate(枚举) 对象

?

1

2

3

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')]

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

# 普通for循环:

i = 0

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

for elementin seq:

print( i,seq[i])

i +=1

'''

one

two

three

'''

# for 循环使用 enumerate

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

for j, elementin enumerate(seq1):

print (j, element)

'''

one

two

three

'''

获取文件的编码方式

?

1

2

3

4

5

6

7

8

9

import chardet

def get_file_code(file_path):

withopen(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)

判断文件的编码方式

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

f= open("D:\file_information1.csv","rb")#二进制格式读文件

i= 0

while True:

print(i)

line= f.readline()

if not line:

break

else:

try:

# print(line)

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

line.decode('utf8')

#为了暴露出错误,最好此处不print

except:

print(str(line))

i+= 1

读取首行

?

1

2

3

4

5

6

7

filename='D:\file_information1.csv'

withopen(filename,'r',encoding='utf-8')as f:#自行先判断文件的编码方式

read=f.readlines()

for index,infoin enumerate(read):

if index==0:#这里判断

#这里输出的是字符串类型

print(info)

读取首行之外的所有行

?

1

2

3

4

5

6

filename='D:\file_information1.csv'

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

read=f.readlines()

for index,infoin enumerate(read):

if index !=0:#这里判断

print(info)

读取前10行

?

1

2

3

4

5

6

filename='D:\file_information1.csv'

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

read=f.readlines()

for index,infoin enumerate(read):

if index <10:#这里为索引,是 int 整形

print(index,info)

读取任意行,可根据index索引

?

1

2

3

4

5

6

import csv

filename='D:\file_information1.csv'

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

read=f.readlines()

for index,infoin enumerate(read):

print(index)#自己根据index的数字判断

读取第一和第二列

?

1

2

3

4

5

6

7

import csv

filename='D:\file_information1.csv'

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

read=csv.reader(f)

for index,infoin enumerate(read):

#这里输出的是列表类型

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

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

?

1

2

3

4

5

6

7

import csv

filename='D:\file_information1.csv'

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

read=csv.reader(f)

for index,infoin enumerate(read):

if index!=0:#这里加判断

print(info[:2])

读取最后两列

?

1

2

3

4

5

6

import csv

filename='D:\file_information1.csv'

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

read=csv.reader(f)

for index,infoin enumerate(read):

print(info[-2:])

读第一行的第三列

?

1

2

3

4

5

6

filename='D:\file_information1.csv'

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

read=csv.reader(f)

for index,infoin enumerate(read):

if index==0:

print(info[2:3])

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://www.cnblogs.com/carey9420/p/12167159.html

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值