Writing CSV files in Python (original) (raw)

**CSV (Comma Separated Values) is a simple file format used to store tabular data, such as spreadsheets or databases. Each line of the file represents a data record, with fields separated by commas. This format is popular due to its simplicity and wide support.

Ways to Write CSV Files in Python

Below are the ways by which we can write CSV files in Python:

1. Using csv.DictWriter() :

The csv.DictWriter class maps dictionaries onto output rows, allowing you to write data where each row is represented by a dictionary.

Syntax:

csv.DictWriter(csvfile, fieldnames, restval=”, extrasaction=’raise’, dialect=’excel’, *args, **kwds)

Where:

Example:

Python `

import csv

data = [ {'name': 'Nikhil', 'branch': 'COE', 'year': 2, 'cgpa': 9.0}, {'name': 'Sanchit', 'branch': 'COE', 'year': 2, 'cgpa': 9.1}, {'name': 'Aditya', 'branch': 'IT', 'year': 2, 'cgpa': 9.3}, {'name': 'Sagar', 'branch': 'SE', 'year': 1, 'cgpa': 9.5}, {'name': 'Prateek', 'branch': 'MCE', 'year': 3, 'cgpa': 7.8}, {'name': 'Sahil', 'branch': 'EP', 'year': 2, 'cgpa': 9.1} ]

with open('university_records.csv', 'w', newline='') as csvfile: fieldnames = ['name', 'branch', 'year', 'cgpa'] writer = csv.DictWriter(csvfile, fieldnames=fieldnames) writer.writeheader() writer.writerows(data)

`

In this example, student records are stored as dictionaries, and a CSV file named “university_records.csv” is generated using these dictionaries. The `csv.DictWriter` class is utilized to write the field names as headers and the dictionary data rows to the CSV file.

**Output:

name,branch,year,cgpa
Nikhil,COE,2,9.0
Sanchit,COE,2,9.1
Aditya,IT,2,9.3
Sagar,SE,1,9.5
Prateek,MCE,3,7.8
Sahil,EP,2,9.1

2. Using csv.writer()

The csv.writer class is used to write data directly to a CSV file. It handles the conversion of data to a delimited string format.

Syntax:

csv.writer(csvfile, dialect=’excel’, **fmtparams)

Where:

Example:

Python `

import csv

data = [ ['Name', 'Branch', 'Year', 'CGPA'], ['Nikhil', 'COE', 2, 9.0], ['Sanchit', 'COE', 2, 9.1], ['Aditya', 'IT', 2, 9.3], ['Sagar', 'SE', 1, 9.5], ['Prateek', 'MCE', 3, 7.8], ['Sahil', 'EP', 2, 9.1] ]

with open('university_records.csv', 'w', newline='') as csvfile: writer = csv.writer(csvfile) writer.writerows(data)

`

**Output:

name,branch,year,cgpa
Nikhil,COE,2,9.0
Sanchit,COE,2,9.1
Aditya,IT,2,9.3
Sagar,SE,1,9.5
Prateek,MCE,3,7.8
Sahil,EP,2,9.1

Advanced CSV Writing

i) Creating CSV Files with Pipe Delimiters

To use a delimiter other than a comma, specify the delimiter when creating the writer object.

Python `

import csv

data = [ ['Name', 'Age', 'City'], ['Alice', 25, 'New York'], ['Bob', 30, 'Los Angeles'], ['Charlie', 35, 'Chicago'] ]

with open('data_pipe_delimited.csv', 'w', newline='') as csvfile: writer = csv.writer(csvfile, delimiter='|') writer.writerows(data)

`

Output:

Name|Age|City
Alice|25|New York
Bob|30|Los Angeles
Charlie|35|Chicago

ii) Writing CSV Files with Custom Quoting Characters

We can customize the quoting character used in the CSV file.

Python `

import csv

data = [ ['Nikhil', 'COE', 2, 9.0], ['Sanchit', 'COE', 2, 9.1], ['Aditya', 'IT', 2, 9.3], ['Sagar', 'SE', 1, 9.5], ['Prateek', 'MCE', 3, 7.8], ['Sahil', 'EP', 2, 9.1] ]

with open('students_data.csv', 'w', newline='') as csvfile: writer = csv.writer(csvfile, quotechar='~', delimiter='|', quoting=csv.QUOTE_ALL) writer.writerows(data)

`

Output:

Nikhil|COE|2|9.0
Sanchit|COE|2|9.1
Aditya|IT|2|9.3
Sagar|SE|1|9.5
Prateek|MCE|3|7.8
Sahil|EP|2|9.1

iii) Using ‘escapechar’ in CSV Writer

To handle special characters within the data, we can specify an escape character.

Python `

import csv

data = [ ['Nikhil', 'COE', 2, '9.0'], ['Sanchit', 'COE', 2, '9.1'], ['Aditya', 'IT', 2, '9.3'], ['Sagar', 'SE', 1, '9.5'], ['Prateek', 'MCE', 3, '7.8'], ['Sahil', 'EP', 2, '9.1'] ]

with open('students.csv', 'w', newline='') as csvfile: writer = csv.writer(csvfile, delimiter='|', quotechar='"', escapechar='\') writer.writerows(data)

`

Output:

Nikhil|COE|2|9.0
Sanchit|COE|2|9.1
Aditya|IT|2|9.3
Sagar|SE|1|9.5
Prateek|MCE|3|7.8
Sahil|EP|2|9.1

Write into CSV Files Using csv.writer()

csv.writer class is used to insert data to the CSV file. This class returns a writer object which is responsible for converting the user’s data into a delimited string. A csvfile object should be opened with newline='' otherwise, newline characters inside the quoted fields will not be interpreted correctly.

**Syntax: _csv.writer(csvfile, dialect=’excel’, **fmtparams)

**Parameters:

In this example, a CSV file named “university_records.csv” is created and populated with student records. The file contains fields such as Name, Branch, Year, and CGPA. The data rows for individual students are written to the CSV file, followed by the field names.

Python `

import csv

field names

fields = ['Name', 'Branch', 'Year', 'CGPA']

data rows of csv file

rows = [['Nikhil', 'COE', '2', '9.0'], ['Sanchit', 'COE', '2', '9.1'], ['Aditya', 'IT', '2', '9.3'], ['Sagar', 'SE', '1', '9.5'], ['Prateek', 'MCE', '3', '7.8'], ['Sahil', 'EP', '2', '9.1']]

name of csv file

filename = "university_records.csv"

writing to csv file

with open(filename, 'w') as csvfile: # creating a csv writer object csvwriter = csv.writer(csvfile)

# writing the fields
csvwriter.writerow(fields)

# writing the data rows
csvwriter.writerows(rows)

`

**Output:

Name,Branch,Year,CGPA
Nikhil,COE,2,9.0
Sanchit,COE,2,9.1
Aditya,IT,2,9.3
Sagar,SE,1,9.5
Prateek,MCE,3,7.8
Sahil,EP,2,9.1

Creating CSV Files in Python Having Pipe Delimiter

In this example, a CSV file named “data_pipe_delimited.csv” is generated with data rows separated by pipe (|) delimiters. The file contains information on individuals’ names, ages, and cities.

Python `

import csv

Define the data to be written to the CSV file

data = [ ['Name', 'Age', 'City'], ['Alice', 25, 'New York'], ['Bob', 30, 'Los Angeles'], ['Charlie', 35, 'Chicago'] ]

Specify the file name

filename = 'data_pipe_delimited.csv'

Write data to the CSV file with a pipe delimiter

with open(filename, 'w', newline='') as csvfile: csvwriter = csv.writer(csvfile, delimiter='|') csvwriter.writerows(data)

print(f"Data has been written to {filename}")

`

**Output:

Name|Age|City
Alice|25|New York
Bob|30|Los Angeles
Charlie|35|Chicago

Write CSV Files with Quotes

In this example, a CSV file named “students_data.csv” is generated with each field enclosed in double quotes. The file contains information about students’ names, branches, years, and CGPA scores.

Python `

import csv

Define the rows data

rows = [ ['Nikhil', 'COE', '2', '9.0'], ['Sanchit', 'COE', '2', '9.1'], ['Aditya', 'IT', '2', '9.3'], ['Sagar', 'SE', '1', '9.5'], ['Prateek', 'MCE', '3', '7.8'], ['Sahil', 'EP', '2', '9.1'] ]

Specify the file name

filename = 'students_data.csv'

Write the rows data to the CSV file with quotes around each field

with open(filename, 'w', newline='') as csvfile: csvwriter = csv.writer(csvfile, quoting=csv.QUOTE_ALL) csvwriter.writerows(rows)

print(f"Data has been written to {filename}")

`

**Output:

"Nikhil","COE","2","9.0"
"Sanchit","COE","2","9.1"
"Aditya","IT","2","9.3"
"Sagar","SE","1","9.5"
"Prateek","MCE","3","7.8"
"Sahil","EP","2","9.1"

Writing CSV Files with Custom Quoting Character

In this example, a CSV file named “students_data.csv” is created with fields separated by pipe (|) delimiters and enclosed in tildes (~). The data includes student names, branches, years, and CGPA scores.

Python `

import csv

rows = [ ['Nikhil', 'COE', '2', '9.0'], ['Sanchit', 'COE', '2', '9.1'], ['Aditya', 'IT', '2', '9.3'], ['Sagar', 'SE', '1', '9.5'], ['Prateek', 'MCE', '3', '7.8'], ['Sahil', 'EP', '2', '9.1'] ]

filename = 'students_data.csv'

with open(filename, 'w', newline='') as file: writer = csv.writer(file, quoting=csv.QUOTE_NONNUMERIC, delimiter='|', quotechar='~') writer.writerows(rows)

print(f"Data has been written to {filename}")

`

**Output:

Nikhil|COE|2|9.0
Sanchit|COE|2|9.1
Aditya|IT|2|9.3
Sagar|SE|1|9.5
Prateek|MCE|3|7.8
Sahil|EP|2|9.1

Using ”escapechar” in CSV

In this example, a CSV file named “students.csv” is generated with fields separated by pipe (|) delimiters. The quotechar is set to double quotes (") and the escapechar is set to backslash (\\), allowing for proper handling of quotes within the data.

Python `

import csv

rows = [ ['Nikhil', 'COE', '2', '9.0'], ['Sanchit', 'COE', '2', '9.1'], ['Aditya', 'IT', '2', '9.3'], ['Sagar', 'SE', '1', '9.5'], ['Prateek', 'MCE', '3', '7.8'], ['Sahil', 'EP', '2', '9.1'] ]

filename = 'students.csv'

with open(filename, 'w', newline='') as file: writer = csv.writer(file, quoting=csv.QUOTE_NONE, delimiter='|', quotechar='"', escapechar='\') writer.writerows(rows)

print(f"Data has been written to {filename}")

`

**Output:

Nikhil|COE|2|9.0
Sanchit|COE|2|9.1
Aditya|IT|2|9.3
Sagar|SE|1|9.5
Prateek|MCE|3|7.8
Sahil|EP|2|9.1

Conclusion

Writing CSV files in Python is a straightforward and flexible process, thanks to the csv module. Whether you are working with simple lists, dictionaries, or need to handle more complex formatting requirements such as custom delimiters or quoting characters, the csv.writer and csv.DictWriter classes provide the tools you need.

Key points to remember:

By understanding and utilizing these methods, you can efficiently export your data into CSV files, making it accessible for various applications, from data analysis to data sharing.

Writing CSV files in Python – FAQs

What modules are used to write CSV files in Python?

The primary modules used to write CSV files in Python are:

How to add headers to a CSV file in Python?

When using the csv module, you can add headers by writing them as the first row of the CSV file.

**Example with the csv module:

import csv

headers = ['Name', 'Age', 'City']
rows = [
['Alice', 30, 'New York'],
['Bob', 25, 'Los Angeles']
]

with open('data_with_headers.csv', mode='w', newline='') as file:
writer = csv.writer(file)
writer.writerow(headers) # Write headers
writer.writerows(rows) # Write data rows

**Example with the pandas module:

import pandas as pd

data = {
'Name': ['Alice', 'Bob'],
'Age': [30, 25],
'City': ['New York', 'Los Angeles']
}

df = pd.DataFrame(data)
df.to_csv('data_with_headers_pandas.csv', index=False)

How to handle various data types when writing CSV in Python?

When writing CSV files with the csv module, data is written as strings. To handle various data types, you should ensure that non-string data types are converted to strings before writing.

**Example with the csv module:

import csv

data = [
['Alice', 30, 3.5, True],
['Bob', 25, 4.2, False]
]

with open('data_types.csv', mode='w', newline='') as file:
writer = csv.writer(file)
writer.writerow(['Name', 'Age', 'Score', 'Active'])
for row in data:
writer.writerow([str(item) for item in row])

**Example with the pandas module:

import pandas as pd

data = {
'Name': ['Alice', 'Bob'],
'Age': [30, 25],
'Score': [3.5, 4.2],
'Active': [True, False]
}

df = pd.DataFrame(data)
df.to_csv('data_types_pandas.csv', index=False)

How to append data to an existing CSV file in Python?

When using the csv module, you can open the file in append mode to add new rows without overwriting the existing content.

**Example with the csv module:

import csv

new_rows = [
['Charlie', 28, 'Chicago'],
['Diana', 35, 'Houston']
]

with open('data_with_headers.csv', mode='a', newline='') as file:
writer = csv.writer(file)
writer.writerows(new_rows)

**Example with the pandas module:

import pandas as pd

new_data = {
'Name': ['Charlie', 'Diana'],
'Age': [28, 35],
'City': ['Chicago', 'Houston']
}

new_df = pd.DataFrame(new_data)
new_df.to_csv('data_with_headers_pandas.csv', mode='a', header=False, index=False)

In both examples, mode='a' is used to open the file in append mode, and header=False ensures that the column headers are not written again when using pandas.