Home >Backend Development >Python Tutorial >An example of how python reads a csv file and puts the file into a list
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()
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!