Python Tutorial - Master Python Programming For Beginners from Scratch

  • Home
  • Getting Started
  • Basics
  • OOP
  • Advanced Python
  • Tkinter
Home » Python Basics » Python Write CSV File

Python Write CSV File

Summary: in this tutorial, you’ll learn how to write data into a CSV file using the built-in csv module.

Steps for writing a CSV file

To write data into a CSV file, you follow these steps:

  • First, open the CSV file for writing (w mode) by using the open() function.
  • Second, create a CSV writer object by calling the writer() function of the csv module.
  • Third, write data to CSV file by calling the writerow() or writerows() method of the CSV writer object.
  • Finally, close the file once you complete writing data to it.

The following code illustrates the above steps:

import csv # open the file in the write mode f = open('path/to/csv_file', 'w') # create the csv writer writer = csv.writer(f) # write a row to the csv file writer.writerow(row) # close the file f.close()
Code language: Python (python)

It’ll be shorter if you use the with statement so that you don’t need to call the close() method to explicitly close the file:

import csv # open the file in the write mode with open('path/to/csv_file', 'w') as f: # create the csv writer writer = csv.writer(f) # write a row to the csv file writer.writerow(row)
Code language: PHP (php)

If you’re dealing with non-ASCII characters, you need to specify the character encoding in the open function.

The following illustrates how to write UTF-8 characters to a CSV file:

import csv # open the file in the write mode with open('path/to/csv_file', 'w', encoding='UTF8') as f: # create the csv writer writer = csv.writer(f) # write a row to the csv file writer.writerow(row)
Code language: PHP (php)

Writing to CSV files example

The following example shows how to write data to the CSV file:

import csv header = ['name', 'area', 'country_code2', 'country_code3'] data = ['Afghanistan', 652090, 'AF', 'AFG'] with open('countries.csv', 'w', encoding='UTF8') as f: writer = csv.writer(f) # write the header writer.writerow(header) # write the data writer.writerow(data)
Code language: PHP (php)

If you open the countries.csv, you’ll see the one issue. It has an additional blank line between two subsequent rows:

To remove the blank line, you need to pass the keyword argument newline='' to the open() function like this:

import csv header = ['name', 'area', 'country_code2', 'country_code3'] data = ['Afghanistan', 652090, 'AF', 'AFG'] with open('countries.csv', 'w', encoding='UTF8', newline='') as f: writer = csv.writer(f) # write the header writer.writerow(header) # write the data writer.writerow(data)
Code language: PHP (php)

Output:

Writing multiple rows to CSV files

To write multiple rows to a CSV file at once, you use the writerows() method of the CSV writer object.

The following illustrates how to use the writerows() method to write multiple rows into the countries.csv file:

import csv header = ['name', 'area', 'country_code2', 'country_code3'] data = [ ['Albania', 28748, 'AL', 'ALB'], ['Algeria', 2381741, 'DZ', 'DZA'], ['American Samoa', 199, 'AS', 'ASM'], ['Andorra', 468, 'AD', 'AND'], ['Angola', 1246700, 'AO', 'AGO'] ] with open('countries.csv', 'w', encoding='UTF8', newline='') as f: writer = csv.writer(f) # write the header writer.writerow(header) # write multiple rows writer.writerows(data)
Code language: PHP (php)

Writing to CSV files using the DictWriter class

If each row of the CSV file is a dictionary, you can use the DictWriter class of the csv module to write the dictionary to the CSV file.

The example illustrates how to use the DictWriter class to write data to a CSV file:

import csv # csv header fieldnames = ['name', 'area', 'country_code2', 'country_code3'] # csv data rows = [ {'name': 'Albania', 'area': 28748, 'country_code2': 'AL', 'country_code3': 'ALB'}, {'name': 'Algeria', 'area': 2381741, 'country_code2': 'DZ', 'country_code3': 'DZA'}, {'name': 'American Samoa', 'area': 199, 'country_code2': 'AS', 'country_code3': 'ASM'} ] with open('countries.csv', 'w', encoding='UTF8', newline='') as f: writer = csv.DictWriter(f, fieldnames=fieldnames) writer.writeheader() writer.writerows(rows)
Code language: PHP (php)

How it works.

  • First, define variables that hold the field names and data rows of the CSV file.
  • Next, open the CSV file for writing by calling the open() function.
  • Then, create a new instance of the DictWriter class by passing the file object (f) and fieldnames argument to it.
  • After that, write the header for the CSV file by calling the writeheader() method.
  • Finally, write data rows to the CSV file using the writerows() method.

Summary

  • Use the CSV Writer or the DictWriter class to write data to a CSV file.
  • Was this tutorial helpful ?
  • YesNo
Previous Python Read CSV File
Next Python pip

Getting Started

  • What is Python
  • Install Python
  • Setup VS Code for Python
  • Develop Python Hello World Program

Python Fundamentals

  • Python Syntax
  • Variables
  • Strings
  • Numbers
  • Boolean
  • Constants
  • Comments
  • Type Conversion

Control Flow

  • if…else
  • Ternary Operator
  • for Loop
  • while Loop
  • break
  • continue
  • pass

Functions

  • Python Functions
  • Default Parameters
  • Keyword Arguments
  • Recursive Functions
  • Lambda Expressions
  • Function Docstrings

Python List

  • List
  • Tuple
  • Sorting a List in Place: sort()
  • Sorting a List: sorted()
  • Slicing a List: [::]
  • Unpacking a List
  • Iterating over a List: for loop
  • Finding Index of an Element: index()
  • Iterables
  • Transform List Elements: map()
  • Filtering List Elements: filter()
  • Reducing List Elements: reduce()
  • List Comprehensions

Python Dictionary

  • Dictionary
  • Dictionary Comprehension

Python Set

  • Set
  • Set Comprehension
  • Union of Sets
  • Intersection of Sets
  • Difference between Sets
  • Symmetric Difference of Sets
  • Subset
  • Superset
  • Disjoint Sets

Exception Handling

  • try…except
  • try…except…finally
  • try…except…else

Python Loop with Else Clause

  • for…else
  • while…else
  • do…while Emulation

More on Functions

  • Unpacking Tuples
  • The *args Parameters
  • The **kwargs Parameters
  • Partial Functions

Modules

  • Modules
  • Module Search Path
  • Python __name__
  • Packages

File I/O

  • Reading a Text File
  • Writing to a Text File
  • Creating a Text File
  • Checking If a File Exists
  • Reading from a CSV File
  • Writing to a CSV File

Managing Third-party Packages

  • Python Package Index (PyPI) & PIP
  • Python Virtual Environments
  • Install pipenv on Windows
  • Using pipenv Tool

About pythontutorial.net

Pythontutorial.net helps you master Python programming from scratch fast.

Site Links

  • Home
  • Contact
  • About
  • Privacy Policy

Recent Python Tutorials

  • Python Decimal
  • Python Rounding
  • Python Float to Int
  • Python float
  • Tkinter StringVar

Python References

  • Python String Methods
  • Python MySQL
  • Python PostgreSQL
  • Python Oracle
  • Python SQLite

Copyright © 2021 · by pythontutorial.net. All rights reserved. Log in