这篇文章主要介绍了python3读取csv文件任意行列代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 读取每一行 reader
          这篇文章主要介绍了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 <10: #这里为索引,是 int 整形
      print(index,info)
读取任意行,可根据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])
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持易盾网络。
