Back to Python
2026-01-295 min read

Example 2: Writing Multiple Rows with writerows() (Python Programming)

Learn Example 2: Writing Multiple Rows with writerows() (Python Programming) step by step with clear examples and exercises.

Title: Writing Multiple Rows with writerows() (Python Programming)

Why This Matters

In real-world applications, Python developers often need to write data to CSV files efficiently. The writerows() function in the csv module allows us to write multiple rows at once, improving performance and reducing code complexity. Understanding this technique is crucial for handling large datasets and meeting project requirements effectively.

The writerows() function can significantly reduce the time taken to write data to a CSV file, especially when dealing with large datasets. By writing multiple rows at once instead of iterating through each row individually, we can save valuable processing time.

Prerequisites

Before diving into writing multiple rows with writerows(), you should be familiar with:

  1. Python basics (variables, data types, operators)
  2. Reading CSV files using the csv module in Python
  3. List comprehensions and list manipulation
  4. Basic file handling in Python
  5. Understanding how to work with dictionaries in Python
  6. Familiarity with exception handling in Python

Core Concept

The writerows() function in the csv module lets us write multiple rows to a CSV file at once, making it an efficient way to handle large datasets. Here's a step-by-step walkthrough of using writerows().

  1. Import the necessary libraries:
import csv
  1. Create a list of lists (or a 2D list) containing the data you want to write to the CSV file. Each sublist represents a row in the CSV file, and each element within the sublist corresponds to a column:
data = [['Name', 'Age', 'City'],
['Alice', '25', 'New York'],
['Bob', '30', 'Los Angeles'],
['Charlie', '28', 'Chicago']]
  1. Open the CSV file in write mode using the csv.writer() function, and specify the delimiter if necessary:
with open('output.csv', 'w', newline='', encoding='utf-8') as csvfile:
writer = csv.writer(csvfile, delimiter=',') # Specify comma as delimiter
  1. Write the data to the CSV file using the writerows() function:
writer.writerows(data)
  1. Close the file:
csvfile.close()

Now, when you open the 'output.csv' file, you will see the data from your list written in CSV format:

Name,Age,City
Alice,25,New York
Bob,30,Los Angeles
Charlie,28,Chicago

Worked Example

Let's work on a more complex example where we read data from another CSV file and write it to another file using writerows(). In this example, we will handle potential errors by using exception handling.

  1. Read the input data:
try:
with open('input.csv', 'r') as csvfile:
reader = csv.reader(csvfile)
input_data = list(reader)
except FileNotFoundError:
print("Input file not found.")
  1. Prepare the output file and writer:
with open('output.csv', 'w', newline='', encoding='utf-8') as csvfile:
writer = csv.writer(csvfile, delimiter=',') # Specify comma as delimiter
  1. Write the data to the output file using writerows():
try:
writer.writerows(input_data[1:]) # Exclude the header row by indexing [1:]
except Exception as e:
print("Error writing data:", e)
  1. Close the files:
csvfile.close()

Now, when you open 'output.csv', you will see all rows from 'input.csv' except the header row. If there is an error during the process, a helpful error message will be displayed instead of causing the script to crash.

Common Mistakes

  1. Forgetting to exclude the header row when writing data: In the worked example above, remember to index [1:] to exclude the header row.
  2. Not closing the file after writing: Always close the file using csvfile.close() or the with block to ensure proper resource management.
  3. Writing data without properly formatting it: Ensure that the data you write is in the correct format for a CSV file (comma-separated values).
  4. Not handling errors gracefully: Use try-except blocks to handle potential issues like file not found, permission denied, or invalid data.
  5. Writing data with inconsistent data types: Ensure that all columns have consistent data types to avoid issues when reading the CSV file later.
  6. Using a different delimiter than a comma: If your data contains commas, use a different delimiter like a semicolon or tab.
  7. Not specifying an encoding: If your data contains special characters, specify an appropriate encoding (e.g., 'utf-8') when opening the file.

Practice Questions

  1. Write a script to read data from 'input.csv' and write it to 'output.csv', but this time, reverse the order of rows in the output file.
  2. Write a script that reads data from 'input.csv', appends new data (e.g., ['Eve', '35', 'San Francisco']), and writes the updated data to 'output.csv'.
  3. Write a script that reads data from multiple CSV files, merges them into one list, and writes the combined data to a new CSV file.
  4. Write a script that reads data from a CSV file, sorts the rows based on a specific column (e.g., Age), and writes the sorted data to a new CSV file.
  5. Write a script that reads data from a CSV file, filters out rows where the age is greater than 30, and writes the filtered data to a new CSV file.

FAQ

Q: Can I use writerows() with dictionaries instead of lists?

A: Yes, you can convert a list of dictionaries to a list of lists before using writerows(). You may want to ensure that all dictionaries have the same keys in the same order for proper formatting.

Q: What happens if the data contains commas or newline characters?

A: Use the quoting parameter in the csv.writer() function to handle special cases like quoted fields and line breaks. Set quoting=csv.QUOTE_ALL to enclose all fields with quotes, regardless of whether they contain commas or newlines.

Q: How can I write a CSV file with a different delimiter, such as a tab or semicolon?

A: Use the delimiter parameter in the csv.writer() function to specify the desired delimiter. For example, set delimiter='\t' for a tab-separated file.

Q: How can I write a CSV file with different line endings (e.g., LF for Linux, CRLF for Windows)?

A: Use the lineterminator parameter in the csv.writer() function to specify the desired line ending. For example, set lineterminator='\n' for a Unix-style line ending (LF).

Q: How can I write CSV files with UTF-8 encoding?

A: Specify the 'utf-8' encoding when opening the file using the encoding parameter in the open() function, as shown earlier in this example.

Q: Can I use writerows() to write rows from different files at once?

A: Yes, you can read data from multiple files and write it to a single file using list concatenation before calling writerows(). However, keep in mind that the order of the rows may not be preserved if the files are not read in the desired order.

Example 2: Writing Multiple Rows with writerows() (Python Programming) | Python | XQA Learn