Python reads the CSV file and puts the file into a list of instances to explain

Source: Internet
Author: User
Below for you to share a Python read the CSV file and put the file into a list of the examples, with a good reference value, I hope to be helpful to everyone. Come and see it together.

As shown below:

#coding =utf8 "to read the CSV file and place the CSV file in a list. ' Import Csvclass readcsv (object):  def __init__ (self,path= "Demo.csv"):    #创建一个属性用来保存要操作CSV的文件    Self.path=path    Try:      #打开一个csv文件 and give Read permission      self.csvhand=open (Self.path, "R")      #调用csv的reader函数读取csv文件      Self.readcsv=csv.reader (self.csvhand)      #创建一个list用来保存csv中的内容      self.buffer=[]      try:        # Put the contents of the CSV in the 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 ()

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.