Python Write CSV File
CSV File
A CSV stands for "comma-separated values", which is defined as a simple file format that uses specific structuring to arrange tabular data. It stores tabular data such as spreadsheet or database in plain text and has a standard format for data interchange. The CSV file opens into the excel sheet, and the rows and columns data define the standard format.
Python CSV Module Functions
The CSV module work is to handle the CSV files to read/write and get data from specified columns. There are different types of CSV functions, which are as follows:
- csv.field_size_limit - It returns the current maximum field size allowed by the parser.
- csv.get_dialect - Returns the dialect associated with a name.
- csv.list_dialects - Returns the names of all registered dialects.
- csv.reader - Read the data from a CSV file
- csv.register_dialect - It associates dialect with a name, and name must be a string or a Unicode object.
- csv.writer - Write the data to a CSV file
- csv.unregister_dialect - It deletes the dialect, which is associated with the name from the dialect registry. If a name is not a registered dialect name, then an error is being raised.
- csv.QUOTE_ALL - It instructs the writer objects to quote all fields.
- csv.QUOTE_MINIMAL - It instructs the writer objects to quote only those fields which contain special characters such as quotechar, delimiter, etc.
- csv.QUOTE_NONNUMERIC - It instructs the writer objects to quote all the non-numeric fields.
- csv.QUOTE_NONE - It instructs the writer object never to quote the fields.
Writing CSV Files
We can also write any new and existing CSV files in Python by using the csv.writer() module. It is similar to the csv.reader() module and also has two methods, i.e., writer function or the Dict Writer class.
t presents two functions, i.e., writerow() and writerows(). The writerow() function only write one row, and the writerows() function write more than one row.
Dialects
It is defined as a construct that allows you to create, store, and re-use various formatting parameters. It supports several attributes; the most frequently used are:
- Dialect.delimiter: This attribute is used as the separating character between the fields. The default value is a comma (,).
- Dialect.quotechar: This attribute is used to quote fields that contain special characters.
- Dialect.lineterminator: It is used to create new lines, and the default value is '\r\n'.
0 Comments