python3如何读取csv文件任意行列 python3读取csv文件任意行列代码实例

您所在的位置:网站首页 python读取csv数据指定行列 python3如何读取csv文件任意行列 python3读取csv文件任意行列代码实例

python3如何读取csv文件任意行列 python3读取csv文件任意行列代码实例

2022-05-30 17:33| 来源: 网络整理| 查看: 265

本篇文章小编给大家分享一下python3读取csv文件任意行列代码实例,小编觉得挺不错的,现在分享给大家供大家参考,有需要的小伙伴们可以来看看。

读取每一行

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

#读取每一行 filename='D:\file_information1.csv' import csv with open(filename,newline = '',encoding = 'utf-8') as f: #参数encoding = 'utf-8'防止出现乱码 reader = csv.reader(f) #使用csv的reader()方法,创建一个reader对象 csv.reader()读取结果是列表 for row in reader: #遍历reader对象的每一行 print(row)

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

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

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

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

file1 = open('D:\new\abc.txt','a',encoding='utf-8') file1.write('张无忌n') file1.write('宋青书n') file1.close()

enumerate()

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

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

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

start -- 下标起始位置。

返回 enumerate(枚举) 对象

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循环: i = 0 seq = ['one', 'two', 'three'] for element in seq: print( i, seq[i]) i +=1 ''' one two three ''' # for 循环使用 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")#二进制格式读文件 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

读取首行

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行

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

读取任意行,可根据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) #自己根据index的数字判断

读取第一和第二列

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


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3