Home > Backend Development > Python Tutorial > An example of how python reads a csv file and puts the file into a list

An example of how python reads a csv file and puts the file into a list

不言
Release: 2018-04-27 11:07:57
Original
4124 people have browsed it

The following is an example of how python reads a csv file and puts the file into a list. It has a good reference value and I hope it will be helpful to everyone. Come and take a look together

As shown below:

#coding=utf8
'''
读取CSV文件,把csv文件放在一份list中。
'''
import csv
class readCSV(object):
  def __init__(self,path="Demo.csv"):
    #创建一个属性用来保存要操作CSV的文件
    self.path=path
    try:
      #打开一个csv文件,并赋予读的权限
      self.csvHand=open(self.path,"r")
      #调用csv的reader函数读取csv文件
      self.readcsv=csv.reader(self.csvHand)
      #创建一个list用来保存csv中的内容
      self.buffer=[]
      try:
        #把csv中内容存入list 中
        for row in self.readcsv:
          self.buffer.append(row)
      except Exception,e:
        print e
    except Exception,e:
      print e  
    finally:
      #关闭csv文件
      self.csvHand.close()
           
def test():
  csv=readCSV()
  csv.getColAndRowCount()
  
if __name__=="__main__":
  test()
Copy after login

Related recommendations:

How to use Python to read large files

Python reads text content in word

The above is the detailed content of An example of how python reads a csv file and puts the file into a list. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template