Working with CSV files

The CSV format stands for Comma Separated Values. The commas are used to separate the fields in a record. These are commonly used for importing and exporting the format for spreadsheets and databases.

A CSV file is a plain text file that uses a specific type of structuring to arrange tabular data. Python has the  built-in csv module that allows Python to parse these types of files. The csv module can be mostly used to work with data that is exported from spreadsheets, as well as databases in text file format, with fields and records.

The csv module has all of the required functions built-in, as follows:

  • csv.reader: This function is used to return a reader object, which iterates over lines of a CSV file
  • csv.writer: This function is used to return a writer object, which writes data into CSV file
  • csv.register_dialect: This function is used to register a CSV dialect
  • csv.unregister_dialect: This function is used to unregister a CSV dialect
  • csv.get_dialect: This function is used to returns a dialect with a given name
  • csv.list_dialects: This function is used to return all registered dialects
  • csv.field_size_limit: This function is used to return the current maximum field size allowed by the parser

In this section, we are going to look at csv.reader and csv.writer  only.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset