Conversion (24)

CSV (Comma-Separated Values) is a universal file format for storing tabular data, while lists are Python’s fundamental data structure for easy data manipulation. Converting CSV to lists in Python enables seamless data processing, analysis, and integration with other workflows. While Python’s built-in csv module works for basic cases, Spire.XLS for Python simplifies handling structured CSV data with its intuitive spreadsheet-like interface.
This article will guide you through how to use Python to read CSV into lists (and lists of dictionaries), covering basic to advanced scenarios with practical code examples.
Table of Contents:
- Why Choose Spire.XLS for CSV to List Conversion?
- Basic Conversion: CSV to Python List
- Advanced: Convert CSV to List of Dictionaries
- Handle Special Scenarios
- Conclusion
- Frequently Asked Questions
Why Choose Spire.XLS for CSV to List Conversion?
Spire.XLS is a powerful library designed for spreadsheet processing, and it excels at CSV handling for several reasons:
- Simplified Indexing: Uses intuitive 1-based row/column indexing (matching spreadsheet logic).
- Flexible Delimiters: Easily specify custom separators (commas, tabs, semicolons, etc.).
- Structured Access: Treats CSV data as a worksheet, making row/column traversal straightforward.
- Robust Data Handling: Automatically parses numbers, dates, and strings without extra code.
Installation
Before starting, install Spire.XLS for Python using pip:
pip install Spire.XLS
This command installs the latest stable version, enabling immediate use in your projects.
Basic Conversion: CSV to Python List
If your CSV file has no headers (pure data rows), Spire.XLS can directly read rows and convert them to a list of lists (each sublist represents a CSV row).
Step-by-Step Process:
- Import the Spire.XLS module.
- Create a Workbook object and load the CSV file.
- Access the first worksheet (Spire.XLS parses CSV into a worksheet).
- Traverse rows and cells, extracting values into a Python list.
CSV to List Python Code Example:
from spire.xls import *
from spire.xls.common import *
# Initialize Workbook and load CSV
workbook = Workbook()
workbook.LoadFromFile("Employee.csv",",")
# Get the first worksheet
sheet = workbook.Worksheets[0]
# Convert sheet data to a list of lists
data_list = []
for i in range(sheet.Rows.Length):
row = []
for j in range(sheet.Columns.Length):
cell_value = sheet.Range[i + 1, j + 1].Value
row.append(cell_value)
data_list.append(row)
# Display the result
for row in data_list:
print(row)
# Dispose resources
workbook.Dispose()
Output:

If you need to convert the list back to CSV, refer to: Python List to CSV: 1D/2D/Dicts – Easy Tutorial
Advanced: Convert CSV to List of Dictionaries
For CSV files with headers (e.g., name,age,city), converting to a list of dictionaries (where keys are headers and values are row data) is more intuitive for data manipulation.
CSV to Dictionary Python Code Example:
from spire.xls import *
# Initialize Workbook and load CSV
workbook = Workbook()
workbook.LoadFromFile("Customer_Data.csv", ",")
# Get the first worksheet
sheet = workbook.Worksheets[0]
# Extract headers (first row)
headers = []
for j in range(sheet.Columns.Length):
headers.append(sheet.Range[1, j + 1].Value)
# Convert data rows to list of dictionaries
dict_list = []
for i in range(1, sheet.Rows.Length): # Skip header row
row_dict = {}
for j in range(sheet.Columns.Length):
key = headers[j]
value = sheet.Range[i + 1, j + 1].Value
row_dict[key] = value
dict_list.append(row_dict)
# Output the result
for record in dict_list:
print(record)
workbook.Dispose()
Explanation
- Load the CSV: Use LoadFromFile() method of Workbook class.
- Extracting Headers: Pull the first row of the worksheet to use as dictionary keys.
- Map Rows to Dictionaries: For each data row (skipping the header row), create a dictionary where keys are headers and values are cell contents.
Output:

Handle Special Scenarios
CSV with Custom Delimiters (e.g., Tabs, Semicolons)
To process CSV files with delimiters other than commas (e.g., tab-separated TSV files), specify the delimiter in LoadFromFile:
# Load a tab-separated file
workbook.LoadFromFile("data.tsv", "\t")
# Load a semicolon-separated file
workbook.LoadFromFile("data_eu.csv", ";")
Clean Empty Values
Empty cells in the CSV are preserved as empty strings ('') in the list. To replace empty strings with a custom value (e.g., "N/A"), modify the cell value extraction:
cell_value = sheet.Range[i + 1, j + 1].Value or "N/A"
Conclusion
Converting CSV to lists in Python using Spire.XLS is efficient, flexible, and beginner-friendly. Whether you need a list of lists for raw data or a list of dictionaries for structured analysis, this library handles parsing, indexing, and resource management efficiently. By following the examples above, you can integrate this conversion into data pipelines, analysis scripts, or applications with minimal effort.
For more advanced features (e.g., CSV to Excel conversion, batch processing), you can visit the Spire.XLS for Python documentation.
Frequently Asked Questions
Q1: Is Spire.XLS suitable for large CSV files?
A: Spire.XLS handles large files efficiently, but for very large datasets (millions of rows), consider processing in chunks or using specialized big data tools. For typical business datasets, it performs excellently.
Q2: How does this compare to using pandas for CSV to list conversion?
A: Spire.XLS offers more control over the parsing process and doesn't require additional data science dependencies. While pandas is great for analysis, Spire.XLS is ideal when you need precise control over CSV parsing or are working in environments without pandas.
Q3: How do I handle CSV files with headers when converting to lists?
A: For headers, use the dictionary conversion method. Extract the first row as headers, then map subsequent rows to dictionaries where keys are header values. This preserves column meaning and enables easy data access by column name.
Q4: How do I convert only specific columns from my CSV to a list?
A: Modify the inner loop to target specific columns:
# Convert only columns 1 and 3 (index 0 and 2)
target_columns = [0, 2]
for i in range(sheet.Rows.Length):
row = []
for j in target_columns:
cell_value = sheet.Range[i + 1, j + 1].Value
row.append(cell_value)
data_list.append(row)
Python TXT to CSV Tutorial | Convert TXT Files to CSV in Python
2025-10-15 07:42:33 Written by zaki zou
When working with data in Python, converting TXT files to CSV is a common and essential task for data analysis, reporting, or sharing data between applications. TXT files often store unstructured plain text, which can be difficult to process, while CSV files organize data into rows and columns, making it easier to work with and prepare for analysis. This tutorial explains how to convert TXT to CSV in Python efficiently, covering single-file conversion, batch conversion, and tips for handling different delimiters.
Table of Contents
- What is a CSV File
- Python TXT to CSV Library - Installation
- Convert a TXT File to CSV in Python (Step-by-Step)
- Automate Batch Conversion of Multiple TXT Files
- Advanced Tips for Python TXT to CSV Conversion
- Conclusion
- FAQs: Python Text to CSV
What is a CSV File?
A CSV (Comma-Separated Values) file is a simple text-based file format used to store tabular data. Each line in a CSV file represents a row, and values within the row are separated by commas (or another delimiter such as tabs or semicolons).
CSV is widely supported by spreadsheet applications, databases, and programming languages like Python. Its simple format makes it easy to import, export, and use across platforms such as Excel, Google Sheets, R, and SQL for data analysis and automation.
An Example CSV File:
Name, Age, City
John, 28, New York
Alice, 34, Los Angeles
Bob, 25, Chicago
Python TXT to CSV Library - Installation
To perform TXT to CSV conversion in Python, we will use Spire.XLS for Python, a powerful library for creating and manipulating Excel and CSV files, without requiring Microsoft Excel to be installed.

You can install it directly from PyPI with the following command:
pip install Spire.XLS
If you need instructions for the installation, visit the guide on How to Install Spire.XLS for Python.
Convert a TXT File to CSV in Python (Step-by-Step)
Converting a text file to CSV in Python is straightforward. You can complete the task in just a few steps. Below is a basic outline of the process:
- Prepare and read the text file: Load your TXT file and read its content line by line.
- Split the text data: Separate each line into fields using a specific delimiter such as a space, tab, or comma.
- Write data to CSV: Use Spire.XLS to write the processed data into a new CSV file.
- Verify the output: Check the CSV in Excel, Google Sheets, or a text editor.
The following code demonstrates how to export a TXT file to CSV using Python:
from spire.xls import *
# Read the txt file
with open("data.txt", "r", encoding="utf-8") as file:
lines = file.readlines()
# Process each line by splitting based on spaces (you can change the delimiter if needed)
processed_data = [line.strip().split() for line in lines]
# Create an Excel workbook
workbook = Workbook()
# Get the first worksheet
sheet = workbook.Worksheets[0]
# Write data from the processed list to the worksheet
for row_num, row_data in enumerate(processed_data):
for col_num, cell_data in enumerate(row_data):
# Write data into cells
sheet.Range[row_num + 1, col_num + 1].Value = cell_data
# Save the sheet as CSV file (UTF-8 encoded)
sheet.SaveToFile("TxtToCsv.csv", ",", Encoding.get_UTF8())
# Dispose the workbook to release resources
workbook.Dispose()
TXT to CSV Output:

If you are also interested in converting a TXT file to Excel, see the guide on converting TXT to Excel in Python.
Automate Batch Conversion of Multiple TXT Files
If you have multiple text files that you want to convert to CSV automatically, you can loop through all .txt files in a folder and convert them one by one.
The following code demonstrates how to batch convert multiple TXT files to CSV in Python:
import os
from spire.xls import *
# Folder containing TXT files
input_folder = "txt_files"
output_folder = "csv_files"
# Create output folder if it doesn't exist
os.makedirs(output_folder, exist_ok=True)
# Function to process a single TXT file
def convert_txt_to_csv(file_path, output_path):
# Read the TXT file
with open(file_path, "r", encoding="utf-8") as f:
lines = f.readlines()
# Process each line (split by space, modify if your delimiter is different)
processed_data = [line.strip().split() for line in lines if line.strip()]
# Create workbook and access the first worksheet
workbook = Workbook()
sheet = workbook.Worksheets[0]
# Write processed data into the sheet
for row_num, row_data in enumerate(processed_data):
for col_num, cell_data in enumerate(row_data):
sheet.Range[row_num + 1, col_num + 1].Value = cell_data
# Save the sheet as CSV with UTF-8 encoding
sheet.SaveToFile(output_path, ",", Encoding.get_UTF8())
workbook.Dispose()
print(f"Converted '{file_path}' -> '{output_path}'")
# Loop through all TXT files in the folder and convert each to a CSV file with the same file name
for filename in os.listdir(input_folder):
if filename.lower().endswith(".txt"):
input_path = os.path.join(input_folder, filename)
output_name = os.path.splitext(filename)[0] + ".csv"
output_path = os.path.join(output_folder, output_name)
convert_txt_to_csv(input_path, output_path)
Advanced Tips for Python TXT to CSV Conversion
Converting text files to CSV can involve variations in text file layout and potential errors, so these tips will help you handle different scenarios more effectively.
1. Handle Different Delimiters
Not all text files use spaces to separate values. If your TXT file uses tabs, commas, or other characters, you can adjust the split() function to match the delimiter.
- For tab-separated files (.tsv):
processed_data = [line.strip().split('\t') for line in lines]
- For comma-separated files:
processed_data = [line.strip().split(',') for line in lines]
- For custom delimiters (e.g., |):
processed_data = [line.strip().split('|') for line in lines]
This ensures that your data is correctly split into columns before writing to CSV.
2. Add Error Handling
When reading or writing files, it's a good practice to use try-except blocks to catch potential errors. This makes your script more robust and prevents unexpected crashes.
try:
# your code here
except Exception as e:
print("Error:", e)
Tip: Use descriptive error messages to help understand the problem.
- Skip Empty Lines
Sometimes, text files may have empty lines. You can filter out the blank lines to avoid creating empty rows in CSV:
processed_data = [line.strip().split() for line in lines if line.strip()]
Conclusion
In this article, you learned how to convert a TXT file to CSV format in Python using Spire.XLS for Python. This conversion is an essential step in data preparation, helping organize raw text into a structured format suitable for analysis, reporting, and sharing. With Spire.XLS for Python, you can automate the text to CSV conversion, handle different delimiters, and efficiently manage multiple text files.
If you have any questions or need technical assistance about Python TXT to CSV conversion, visit our Support Forum for help.
FAQs: Python Text to CSV
Q1: Can I convert TXT files to CSV without Microsoft Excel installed?
A1: Yes. Spire.XLS for Python works independently of Microsoft Excel, allowing you to create and export CSV files directly.
Q2: How to batch convert multiple TXT files to CSV in Python?
A2: Use a loop to read all TXT files in a folder and apply the conversion function for each. The tutorial includes a ready-to-use Python example for batch conversion.
Q3: How do I handle empty lines or inconsistent rows in TXT files when converting to CSV?
A3: Filter out empty lines during processing and implement checks for consistent column counts to avoid errors or blank rows in the output CSV.
Q4: How do I convert TXT files with tabs or custom delimiters to CSV in Python?
A4: You can adjust the split() function in your Python script to match the delimiter in your TXT file-tabs (\t), commas, or custom characters-before writing to CSV.

In today's data-driven world, Python developers frequently need to convert lists (a fundamental Python data structure) into Excel spreadsheets. Excel remains the standard for data presentation, reporting, and sharing across industries. Whether you're generating reports, preparing data for analysis, or sharing information with non-technical stakeholders, the ability to efficiently export Python lists to Excel is a valuable skill.
While lightweight libraries like pandas can handle basic exports, Spire.XLS for Python gives you full control over Excel formatting, styles, and file generation – all without requiring Microsoft Excel. In this comprehensive guide, we'll explore how to use the library to convert diverse list structures into Excel in Python, complete with detailed examples and best practices.
- Why Convert Python Lists to Excel?
- Installation Guide
- Basic – Convert a Simple Python List to Excel
- Convert Nested Lists to Excel in Python
- Convert a List of Dictionaries to Excel
- 4 Tips to Optimize Your Excel Outputs
- Conclusion
- FAQs
Why Convert Python Lists to Excel?
Lists in Python are versatile for storing structured or unstructured data, but Excel offers advantages in:
- Collaboration: Excel is universally used, and stakeholders can edit, sort, or filter data without Python knowledge.
- Reporting: Add charts, pivot tables, or summaries to Excel after export.
- Compliance: Many industries require data in Excel for audits or record-keeping.
- Visualization: Excel’s formatting tools (colors, borders, headers) make data easier to read than raw Python lists.
Whether you’re working with sales data, user records, or survey results, writing lists to Excel in Python ensures your data is accessible and professional.
Installation Guide
To get started with Spire.XLS for Python, install it using pip:
pip install Spire.XLS
The Python Excel library supports Excel formats like .xls or .xlsx and lets you customize formatting (bold headers, column widths, colors), perfect for production-ready files.
To fully experience the capabilities of Spire.XLS for Python, you can request a free 30-day trial license here.
Basic – Convert a Simple Python List to Excel
For a basic one-dimensional list, iterate through the items and write them to consecutive cells in a single column.
This code example converts a list of text strings into a single column. If you need to convert a list of numeric values, you can set their number format before saving.
from spire.xls import *
from spire.xls.common import *
# Create a Workbook object
workbook = Workbook()
# Clear the default sheets
workbook.Worksheets.Clear()
# Add a new worksheet
worksheet = workbook.Worksheets.Add("Simple List")
# Sample list
data_list = ["Alexander", "Bob", "Charlie", "Diana", "Eve"]
# Write list data to Excel cells (starting from row 1, column 1)
for index, value in enumerate(data_list):
worksheet.Range[index + 1, 1].Value = value
# Set column width for better readability
worksheet.Range[1, 1].ColumnWidth = 15
# Save the workbook
workbook.SaveToFile("SimpleListToExcel.xlsx", ExcelVersion.Version2016)
workbook.Dispose()
If you need to write the list in a single row, use the following:
for index, value in enumerate(data_list):
worksheet.Range[1, index + 1].Value = value
Output: A clean Excel file with one column of names, properly spaced.

Convert Nested Lists to Excel in Python
Nested lists (2D Lists) represent tabular data with rows and columns, making them perfect for direct conversion to Excel tables. Let’s convert a nested list of employee data (name, age, department) to an Excel table.
from spire.xls import *
from spire.xls.common import *
# Create a Workbook object
workbook = Workbook()
# Clear the default sheets
workbook.Worksheets.Clear()
# Add a new worksheet
worksheet = workbook.Worksheets.Add("Employee Data")
# Nested list (rows: [Name, Age, Department])
employee_data = [
["Name", "Age", "Department"], # Header row
["Alexander", 30, "HR"],
["Bob", 28, "Engineering"],
["Charlie", 35, "Marketing"],
["Diana", 29, "Finance"]
]
# Write nested list to Excel
for row_idx, row_data in enumerate(employee_data):
for col_idx, value in enumerate(row_data):
if isinstance(value, int):
worksheet.Range[row_idx + 1, col_idx + 1].NumberValue = value
else:
worksheet.Range[row_idx + 1, col_idx + 1].Value = value
# Format header row
worksheet.Range["A1:C1"].Style.Font.IsBold = True
worksheet.Range["A1:C1"].Style.Color = Color.get_Yellow()
# Set column widths
worksheet.Range[1, 1].ColumnWidth = 10
worksheet.Range[1, 2].ColumnWidth = 6
worksheet.Range[1, 3].ColumnWidth = 15
# Save the workbook
workbook.SaveToFile("NestedListToExcel.xlsx", ExcelVersion.Version2016)
workbook.Dispose()
Explanation:
- Nested List Structure: The first sub-list acts as headers, and subsequent sub-lists are data rows.
- 2D Loop: We use nested loops to write each row and column to Excel cells.
Output: An Excel table with bold yellow headers and correctly typed data.

To make your Excel files more professional, you can add cell borders, set conditional formatting, or apply other formatting options with Spire.XLS for Python.
Convert a List of Dictionaries to Excel
Lists of dictionaries are common in Python for storing structured data with labeled fields. This example converts a list of dictionaries (e.g., customer records) to Excel and auto-extracts headers from dictionary keys.
from spire.xls import *
from spire.xls.common import *
# Create a Workbook object
workbook = Workbook()
# Clear the default sheets
workbook.Worksheets.Clear()
# Add a new worksheet
worksheet = workbook.Worksheets.Add("Customer Data")
# List of dictionaries
customers = [
{"ID": 101, "Name": "John Doe", "Email": "john@example.com"},
{"ID": 102, "Name": "Jane Smith", "Email": "jane@example.com"},
{"ID": 103, "Name": "Mike Johnson", "Email": "mike@example.com"}
]
# Extract headers from dictionary keys
headers = list(customers[0].keys())
# Write headers to row 1
for col, header in enumerate(headers):
worksheet.Range[1, col + 1].Value = header
worksheet.Range[1, col + 1].Style.Font.IsBold = True # Bold headers
# Write data rows
for row, customer in enumerate(customers, start=2): # Start from row 2
for col, key in enumerate(headers):
value = customer[key]
if isinstance(value, (int, float)):
worksheet.Range[row, col + 1].NumberValue = value
else:
worksheet.Range[row, col + 1].Value = value
# Adjust column widths
worksheet.AutoFitColumn(2)
worksheet.AutoFitColumn(3)
# Save the file
workbook.SaveToFile("CustomerDataToExcel.xlsx", ExcelVersion.Version2016)
workbook.Dispose()
Why This Is Useful:
- Auto-Extracted Headers: Saves time. No need to retype headers like “ID” or “Email”.
- Auto-Fit Columns: Excel automatically adjusts column width to fit the longest text.
- Scalable: Works for large lists of dictionaries (e.g., 1000+ customers).
Output: Excel file with headers auto-created, data types preserved, and columns automatically sized.

4 Tips to Optimize Your Excel Outputs
- Preserve Data Types: Always use NumberValue for numbers (avoids issues with Excel calculations later).
- Auto-Fit Columns: Use worksheet.AutoFitColumn() to skip manual width adjustments.
- Name Worksheets Clearly: Instead of “Sheet1”, use names like “Q3 Sales” to make files user-friendly.
- Dispose of Workbooks: Always call workbook.Dispose() to free memory (critical for large datasets).
Conclusion
Converting lists to Excel in Python is a critical skill for data professionals, and Spire.XLS makes it easy to create polished, production-ready files. Whether you’re working with simple lists, nested data, or dictionaries, the examples above can be adapted to your needs.
For even more flexibility (e.g., adding charts or formulas), explore Spire.XLS’s documentation.
FAQs for List to Excel Conversion
Q1: How is Spire.XLS different from pandas for converting lists to Excel?
A: Pandas is great for quick, basic exports, but it lacks fine-grained control over Excel formatting. Spire.XLS is better when you need:
- Custom styles (colors, fonts, borders).
- Advanced Excel features (freeze panes, conditional formatting, charts).
- Standalone functionality (no Excel installation required).
Q2: How do I save my Excel file in different formats?
A: Use the ExcelVersion parameter in SaveToFile:
workbook.SaveToFile("output.xlsx", ExcelVersion.Version2016)
workbook.SaveToFile("output.xls", ExcelVersion.Version97to2003)
Q3: How does Spire.XLS handle different data types?
A: Spire.XLS provides specific properties for different data types:
- Use .Text for strings
- Use .NumberValue for numerical data
- Use .DateTimeValue for dates
- Use .BooleanValue for True/False values
Q4: Why clear default worksheets before adding new ones?
A: Spire.XLS for Python creates default sheets when you create a Workbook. Therefore, if you don't clear it with the Workbook.Worksheets.Clear(), your file will have extra empty sheets.
Q5: My data isn't showing correctly in Excel. What's wrong?
A: Check that you're using 1-based indexing and that your data types match the expected format. Also, verify that you're saving the file before disposing of the workbook.

CSV (Comma-Separated Values) is one of the most widely used formats for data exchange between applications, databases, and programming languages. For Python developers, the need to convert Python lists to CSV format arises constantly - whether exporting application data, generating reports, or preparing datasets for analysis. Spire.XLS for Python streamlines this critical process with an intuitive, reliable approach that eliminates common conversion pitfalls.
This comprehensive guide will explore how to write lists to CSV in Python. You'll discover how to handle everything from simple one-dimensional lists to complex nested dictionaries, while maintaining data integrity and achieving professional-grade output.
Table of Contents:
- Getting Started with Spire.XLS for Python
- Convert 1D List to CSV in Python
- Convert 2D List to CSV in Python
- Convert List of Dictionaries to CSV in Python
- Advanced: Custom Delimiters and Encoding
- Conclusion
- FAQs
Getting Started with Spire.XLS for Python
Why Use Spire.XLS for List-to-CSV Conversion?
While Python's built-in csv module is excellent for simple CSV operations, Spire.XLS offers additional benefits:
- Handles various data types seamlessly
- Lets you customize CSV output (e.g., semicolon delimiters for European locales).
- Can save in multiple file formats (CSV, XLSX, XLS, etc.)
- Works well with both simple and complex data structures
Install via pip
The Spire.XLS for Python lets you create, modify, and save Excel/CSV files programmatically. To use it, run this command in your terminal or command prompt:
pip install Spire.XLS
This command downloads and installs the latest version, enabling you to start coding immediately.
Convert 1D List to CSV in Python
A 1D (one-dimensional) list is a simple sequence of values (e.g., ["Apple", "Banana", "Cherry"]). The following are the steps to write these values to a single row or column in a CSV.
Step 1: Import Spire.XLS Modules
First, import the necessary classes from Spire.XLS:
from spire.xls import *
from spire.xls.common import *
Step 2: Create a Workbook and Worksheet
Spire.XLS uses workbooks and worksheets to organize data. We’ll create a new workbook and add a new worksheet:
# Create a workbook instance
workbook = Workbook()
# Remove the default worksheet and add a new one
workbook.Worksheets.Clear()
worksheet = workbook.Worksheets.Add()
Step 3: Write 1D List Data to the Worksheet
Choose to write the list to a single row (horizontal) or a single column (vertical).
Example 1: Write 1D List to a Single Row
# Sample 1D list
data_list = ["Apple", "Banana", "Orange", "Grapes", "Mango"]
# Write list to row 1
for i, item in enumerate(data_list):
worksheet.Range[1, i+1].Value = item
Example 2: Write 1D List to a Single Column
# Sample 1D list
data_list = ["Apple", "Banana", "Orange", "Grapes", "Mango"]
# Write list to column 1
for i, item in enumerate(data_list):
worksheet.Range[i + 1, 1].Value = item
Step 4: Save the Worksheet as CSV
Use SaveToFile() to export the workbook to a CSV file. Specify FileFormat.CSV to ensure proper formatting:
# Save as CSV file
workbook.SaveToFile("ListToCSV.csv", FileFormat.CSV)
# Close the workbook to free resources
workbook.Dispose()
Output:

Convert 2D List to CSV in Python
A 2D (two-dimensional) list is a list of lists that represents tabular data. More commonly, you'll work with this type of list, where each inner list represents a row in the CSV file.
Python Code for 2D List to CSV:
from spire.xls import *
from spire.xls.common import *
# Create a workbook instance
workbook = Workbook()
# Remove the default worksheet and add a new one
workbook.Worksheets.Clear()
worksheet = workbook.Worksheets.Add()
# Sample 2D list (headers + data)
data = [
["Name", "Age", "City", "Salary"],
["John Doe", 30, "New York", 50000],
["Jane Smith", 25, "Los Angeles", 45000],
["Bob Johnson", 35, "Chicago", 60000],
["Alice Brown", 28, "Houston", 52000]
]
# Write 2D list to worksheet
for row_index, row_data in enumerate(data):
for col_index, cell_data in enumerate(row_data):
worksheet.Range[row_index + 1, col_index + 1].Value = str(cell_data)
# Save as a CSV file
workbook.SaveToFile("2DListToCSV.csv", FileFormat.CSV)
workbook.Dispose()
Key points:
- Ideal for structured tabular data with headers
- Nested loops handle both rows and columns
- Converting all values to strings ensures compatibility
Output:

The generated CSV can be converted to PDF for secure presentation, or converted to JSON for web/API data exchange.
Convert List of Dictionaries to CSV in Python
Lists of dictionaries are ideal when data has named fields (e.g., [{"Name": "Alice", "Age": 30}, {"Name": "Bob", "Age": 25}]). The dictionary keys become CSV headers, and values become rows.
Python Code for List of Dictionaries to CSV
from spire.xls import *
from spire.xls.common import *
# Create a workbook instance
workbook = Workbook()
# Remove the default worksheet and add a new one
workbook.Worksheets.Clear()
worksheet = workbook.Worksheets.Add()
# Sample 2D list (headers + data)
customer_list = [
{"CustomerID": 101, "Name": "Emma Wilson", "Email": "emma@example.com"},
{"CustomerID": 102, "Name": "Liam Brown", "Email": "liam@example.com"},
{"CustomerID": 103, "Name": "Olivia Taylor", "Email": "olivia@example.com"}
]
# Extract headers (dictionary keys) and write to row 1
if customer_list: # Ensure the list is not empty
headers = list(customer_list[0].keys())
# Write headers
for col_index, header in enumerate(headers):
worksheet.Range[1, col_index + 1].Value = str(header)
# Write dictionary values to rows 2 onwards
for row_index, record in enumerate(customer_list):
for col_index, header in enumerate(headers):
# Safely get value, use empty string if key doesn't exist
value = record.get(header, "")
worksheet.Range[row_index + 2, col_index + 1].Value = str(value)
# Save as CSV file
workbook.SaveToFile("Customer_Data.csv", FileFormat.CSV)
workbook.Dispose()
Key points:
- Extracts headers from the first dictionary's keys
- Uses .get() method to safely handle missing keys
- Maintains column order based on the header row
Output:

Advanced: Custom Delimiters and Encoding
One of the biggest advantages of using Spire.XLS for Python is its flexibility in saving CSV files with custom delimiters and encodings. This allows you to tailor your CSV output for different regions, applications, and data requirements.
To specify the delimiters and encoding, simply change the corresponding parameter in the SaveToFile() method of the Worksheet class. Example:
# Save with different delimiters and encodings
worksheet.SaveToFile("semicolon_delimited.csv", ";", Encoding.get_UTF8())
worksheet.SaveToFile("tab_delimited.csv", "\t", Encoding.get_UTF8())
worksheet.SaveToFile("unicode_encoded.csv", ",", Encoding.get_Unicode())
Conclusion
Converting Python lists to CSV is straightforward with the right approach. Whether you're working with simple 1D lists, structured 2D arrays, or more complex lists of dictionaries, Spire.XLS provides a robust solution. By choosing the appropriate method for your data structure, you can ensure efficient and accurate CSV generation in any application.
For more advanced features and detailed documentation, you can visit the official Spire.XLS for Python documentation.
Frequently Asked Questions (FAQs)
Q1: What are the best practices for list to CSV conversion?
- Validate input data before processing
- Handle exceptions with try-catch blocks
- Test with sample data before processing large datasets
- Clean up resources using Dispose()
Q2: Can I export multiple lists into separate CSV files in one go?
Yes. Loop through your lists and save each as a separate CSV:
lists = {
"fruits": ["Apple", "Banana", "Cherry"],
"scores": [85, 92, 78]
}
for name, data in lists.items():
wb = Workbook()
wb.Worksheets.Clear()
ws = wb.Worksheets.Add(name)
for i, val in enumerate(data):
ws.Range[i + 1, 1].Value = str(val)
wb.SaveToFile(f"{name}.csv", FileFormat.CSV)
wb.Dispose()
Q3: How to format numbers (e.g., currency, decimals) in CSV?
CSV stores numbers as plain text, so formatting must be applied before saving:
ws.Range["A1:A10"].NumberFormat = "$#,##0.00"
This ensures numbers appear as $1,234.56 in the CSV. For more number formatting options, refer to: Set the Number Format in Python
Q4: Does Spire.XLS for Python work on all operating systems?
Yes! Spire.XLS for Python is cross-platform and supports Windows, macOS, and Linux systems.

CSV (Comma-Separated Values) is a widely used format for tabular data. It’s lightweight, easy to generate, and common in reports, logs, exports, and data feeds. However, modern web applications, APIs, and NoSQL databases prefer JSON for its hierarchical structure, flexibility, and compatibility with JavaScript.
Converting CSV to JSON in Python is a practical skill for developers who need to:
- Prepare CSV data for APIs and web services
- Migrate CSV exports into NoSQL databases like MongoDB
- Transform flat CSV tables into nested JSON objects
- Enable data exchange between systems that require hierarchical formats
This step-by-step tutorial shows you how to convert CSV files to JSON in Python, including flat JSON, nested JSON, JSON with grouped data, and JSON Lines (NDJSON). By the end, you’ll be able to transform raw CSV datasets into well-structured JSON ready for APIs, applications, or data pipelines.
Table of Contents
- Why Convert CSV to JSON
- Python CSV to JSON Converter - Installation
- Convert CSV to Flat JSON in Python
- Convert CSV to Nested JSON in Python
- Convert CSV to JSON with Grouped Data in Python
- Convert CSV to JSON Lines (NDJSON) in Python
- Handle Large CSV Files to JSON Conversion
- Best Practices for CSV to JSON Conversion
- Conclusion
- FAQs
Why Convert CSV to JSON?
CSV files are lightweight and tabular, but they lack hierarchy. JSON allows structured, nested data ideal for APIs and applications. Converting CSV to JSON enables:
- API Integration: Most APIs prefer JSON over CSV
- Flexible Data Structures: JSON supports nested objects
- Web Development: JSON works natively with JavaScript
- Database Migration: NoSQL and cloud databases often require JSON
- Automation: Python scripts can process JSON efficiently
Python CSV to JSON Converter – Installation
To convert CSV files to JSON in Python, this tutorial uses Spire.XLS for Python to read CSV files and Python’s built-in json module to handle JSON conversion.
Why Spire.XLS?
It simplifies working with CSV files by allowing you to:
- Load CSV files into a workbook structure for easy access to rows and columns
- Extract and manipulate data efficiently, cell by cell
- Convert CSV to JSON in flat, nested, or NDJSON formats
- Export CSV to Excel, PDF, and other formats if needed
Install Spire.XLS
You can install the library directly from PyPI using pip:
pip install spire.xls
If you need detailed guidance on the installation, refer to this tutorial: How to Install Spire.XLS for Python on Windows.
Once installed, you’re ready to convert CSV data into different JSON formats.
Convert CSV to Flat JSON in Python
Converting a CSV file to flat JSON turns each row into a separate JSON object and uses the first row as keys, making the data organized and easy to work with.
Steps to Convert CSV to Flat JSON
- Load the CSV file into a workbook using Workbook.LoadFromFile.
- Select the worksheet.
- Extract headers from the first row.
- Iterate through each subsequent row to map values to headers.
- Append each row dictionary to a list.
- Write the list to a JSON file using json.dump.
Code Example
from spire.xls import *
import json
# Load the CSV file into a workbook object
workbook = Workbook()
workbook.LoadFromFile("employee.csv", ",")
# Select the desired worksheet
sheet = workbook.Worksheets[0]
# Extract headers from the first row
headers = [sheet.Range[1, j].Text for j in range(1, sheet.LastColumn + 1)]
# Map the subsequent CSV rows to JSON objects
data = []
for i in range(2, sheet.LastRow + 1):
row = {headers[j-1]: sheet.Range[i, j].Text for j in range(1, sheet.LastColumn + 1)}
data.append(row)
# Write JSON to file
with open("output_flat.json", "w", encoding="utf-8") as f:
json.dump(data, f, indent=4, ensure_ascii=False)
# Clean up resources
workbook.Dispose()
Output JSON

Convert CSV to Nested JSON in Python
When a single CSV row contains related columns, you can combine these columns into nested JSON objects. For example, merging the Street and City columns into an Address object. Each CSV row produces one JSON object, which can include one or more nested dictionaries. This approach is ideal for scenarios requiring hierarchical data within a single record, such as API responses or application configurations.
Steps to Convert CSV to Nested JSON
- Load the CSV file and select the worksheet.
- Decide which columns should form a nested object (e.g., street and city).
- Iterate over rows and construct each JSON object with a sub-object for nested fields.
- Append each nested object to a list.
- Write the list to a JSON file with json.dump.
Code Example
from spire.xls import *
import json
# Create a Workbook instance and load the CSV file (using comma as the delimiter)
workbook = Workbook()
workbook.LoadFromFile("data.csv", ",")
# Get the first worksheet from the workbook
sheet = workbook.Worksheets[0]
# List to store the converted JSON data
data = []
# Loop through rows starting from the second row (assuming the first row contains headers)
for i in range(2, sheet.LastRow + 1):
# Map each row into a JSON object, including a nested "Address" object
row = {
"ID": sheet.Range[i, 1].Text, # Column 1: ID
"Name": sheet.Range[i, 2].Text, # Column 2: Name
"Address": { # Nested object for address
"Street": sheet.Range[i, 3].Text, # Column 3: Street
"City": sheet.Range[i, 4].Text # Column 4: City
}
}
# Add the JSON object to the list
data.append(row)
# Write the JSON data to a file with indentation for readability
with open("output_nested.json", "w", encoding="utf-8") as f:
json.dump(data, f, indent=4, ensure_ascii=False)
# Release resources used by the workbook
workbook.Dispose()
Output Nested JSON

Convert CSV to JSON with Grouped Data
When multiple CSV rows belong to the same parent entity, you can group these rows under a single parent object. For example, an order with multiple items can store all items in an items array under one order object. Each parent object has a unique key (like order_id), and its child rows are aggregated into an array. This method is useful for e-commerce orders, data pipelines, or any scenario requiring grouped hierarchical data across multiple rows.
Steps to Convert CSV to JSON with Grouped Data
- Use defaultdict to group rows by a parent key (order_id).
- Iterate rows and append child items to the parent object.
- Convert the grouped dictionary to a list of objects.
- Write the JSON file.
Code Example
from collections import defaultdict
from spire.xls import *
import json
# Create a Workbook instance and load the CSV file (comma-separated)
workbook = Workbook()
workbook.LoadFromFile("orders.csv", ",")
# Get the first worksheet from the workbook
sheet = workbook.Worksheets[0]
# Use defaultdict to store grouped data
# Each order_id maps to a dictionary with customer name and a list of items
data = defaultdict(lambda: {"customer": "", "items": []})
# Loop through rows starting from the second row (skip header row)
for i in range(2, sheet.LastRow + 1):
order_id = sheet.Range[i, 1].Text # Column 1: Order ID
customer = sheet.Range[i, 2].Text # Column 2: Customer
item = sheet.Range[i, 3].Text # Column 3: Item
# Assign customer name (same for all rows with the same order_id)
data[order_id]["customer"] = customer
# Append item to the order's item list
data[order_id]["items"].append(item)
# Convert the grouped dictionary into a list of objects
# Each object contains order_id, customer, and items
result = [{"order_id": oid, **details} for oid, details in data.items()]
# Write the grouped data to a JSON file with indentation for readability
with open("output_grouped.json", "w", encoding="utf-8") as f:
json.dump(result, f, indent=4, ensure_ascii=False)
# Release resources used by the workbook
workbook.Dispose()
Output JSON with Grouped Data

If you're also interested in saving JSON back to CSV, follow our guide on converting JSON to CSV in Python.
Convert CSV to JSON Lines (NDJSON) in Python
JSON Lines (also called NDJSON – Newline Delimited JSON) is a format where each line is a separate JSON object. It is ideal for large datasets, streaming, and big data pipelines.
Why use NDJSON?
- Streaming-friendly: Process one record at a time without loading the entire file into memory.
- Big data compatibility: Tools like Elasticsearch, Logstash, and Hadoop natively support NDJSON.
- Error isolation: If one line is corrupted, the rest of the file remains valid.
Code Example
from spire.xls import *
import json
# Create a Workbook instance and load the CSV file (comma-separated)
workbook = Workbook()
workbook.LoadFromFile("employee.csv", ",")
# Get the first worksheet from the workbook
sheet = workbook.Worksheets[0]
# Extract headers from the first row to use as JSON keys
headers = [sheet.Range[1, j].Text for j in range(1, sheet.LastColumn + 1)]
# Open a file to write JSON Lines (NDJSON) format
with open("output.ndjson", "w", encoding="utf-8") as f:
# Loop through each row in the worksheet, starting from the second row
for i in range(2, sheet.LastRow + 1):
# Map each header to its corresponding cell value for the current row
row = {headers[j - 1]: sheet.Range[i, j].Text for j in range(1, sheet.LastColumn + 1)}
# Write the JSON object to the file followed by a newline
# Each line is a separate JSON object (NDJSON format)
f.write(json.dumps(row, ensure_ascii=False) + "\n")
# Release resources used by the workbook
workbook.Dispose()
Output NDJSON

Handle Large CSV Files to JSON Conversion
For large CSV files, it’s not always efficient to load everything into memory at once. With Spire.XLS, you can still load the file as a worksheet, but instead of appending everything into a list, you can process rows in chunks and write them to JSON incrementally. This technique minimizes memory usage, making it suitable for big CSV to JSON conversion in Python.
Code Example
from spire.xls import *
import json
# Create a Workbook instance and load the CSV file (comma-separated)
workbook = Workbook()
workbook.LoadFromFile("large.csv", ",")
# Get the first worksheet from the workbook
sheet = workbook.Worksheets[0]
# Open a JSON file for writing, with UTF-8 encoding
with open("large.json", "w", encoding="utf-8") as json_file:
json_file.write("[\n") # Start the JSON array
first = True # Flag to handle commas between JSON objects
# Loop through each row in the worksheet, starting from the second row
# (skip the header row)
for i in range(2, sheet.LastRow + 1):
# Create a dictionary mapping each header to its corresponding cell value
row = {sheet.Range[1, j].Text: sheet.Range[i, j].Text
for j in range(1, sheet.LastColumn + 1)}
# Add a comma before the object if it is not the first row
if not first:
json_file.write(",\n")
# Write the JSON object to the file
json.dump(row, json_file, ensure_ascii=False)
first = False # After the first row, set the flag to False
json_file.write("\n]") # End the JSON array
# Release resources used by the workbook
workbook.Dispose()
Best Practices for CSV to JSON Conversion
When converting CSV to JSON in Python, follow these best practices can ensure data integrity and compatibility:
- Always Use CSV headers as JSON keys.
- Handle missing values with null or default values.
- Normalize data types (convert numeric strings to integers or floats).
- Use UTF-8 encoding for JSON files.
- Stream large CSV files row by row to reduce memory usage.
- Validate JSON structure after writing, especially for nested JSON.
Conclusion
Converting CSV to JSON in Python helps you work with data more efficiently and adapt it for modern applications. Using Python and libraries like Spire.XLS for Python, you can:
- Convert flat CSV files into structured JSON objects.
- Organize related CSV data into nested JSON structures.
- Group multiple CSV rows into coherent JSON objects for analysis or APIs.
- Create JSON Lines (NDJSON) for large datasets or streaming scenarios.
- Process large CSV files efficiently without loading everything into memory.
These approaches let you handle CSV data in a way that fits your workflow, making it easier to prepare, share, and analyze data for APIs, applications, or big data pipelines.
FAQs
Q1: How do I convert CSV to JSON with headers in Python?
A1: If your CSV has headers, use the first row as keys and map subsequent rows to dictionaries. With Spire.XLS, you can access sheet.Range[1, j].Text for headers.
Q2: How do I convert CSV to nested JSON in Python?
A2: Identify related columns (e.g., Street and City) and group them into a sub-object when building JSON. See the Nested JSON example above.
Q3: What’s the best way to handle large CSV files when converting to JSON?
A3: Use a streaming approach where each row is processed and written to JSON immediately, instead of storing everything in memory.
Q4: Can Spire.XLS handle CSV files with different delimiters?
A4: Yes, when loading the CSV with Spire.XLS’s LoadFromFile method, specify the delimiter (e.g., "," or ";").
Q5: How to convert JSON back to CSV in Python?
A5: Use Python’s json module to read the JSON file into a list of dictionaries, then write it back to CSV using Spire.XLS for Python for advanced formatting and export options.
Q6: How to convert CSV to JSON Lines (NDJSON) in Python?
A6: JSON Lines (NDJSON) writes each JSON object on a separate line. Stream each CSV row to the output file line by line, which is memory-efficient and compatible with big data pipelines like Elasticsearch or Logstash.

CSV is one of the most common formats for exchanging tabular data because of its simplicity and wide support across platforms. However, when working with structured applications, configurations, or hierarchical data, XML often becomes the preferred choice due to its ability to represent nested relationships and enforce stricter data validation.
In this guide, we’ll explore how to convert CSV files into XML using Spire.XLS for Python. You’ll learn how to convert CSV into both Excel XML format (SpreadsheetML) and standard XML for general use. We’ll also cover how to clean and preprocess real-world CSV files—handling issues like invalid headers, missing values, special characters, and nested fields—so that your XML output is always valid and structured correctly.
Table of Contents:
- Why Convert CSV to XML
- Prerequisites
- Convert CSV to Excel XML Format
- Convert CSV to Standard XML
- Handle Real-World CSV Challenges
- Wrap Up
- FAQs
Why Convert CSV to XML
So why should developers care about converting CSV to XML? Here are some real-world use cases:
- Enterprise Data Migration: Many enterprise applications like ERP or CRM systems require XML input for bulk data import.
- Configuration & Metadata: XML is often used to store structured metadata, while raw data may come in CSV form.
- Interoperability: Some APIs, especially in finance, healthcare, and government sectors, still rely heavily on XML payloads.
- Readable Reports: XML can represent hierarchical data, making it more descriptive compared to flat CSV files.
- Validation: XML can be validated against schemas (XSD), ensuring data integrity—something CSV cannot provide out-of-the-box.
CSV is great for simplicity . XML is great for structure . By converting between them, you get the best of both worlds.
Prerequisites
Before diving into the code, let’s ensure we have the right tools:
- Python 3.7+
- Spire.XLS for Python → A professional library that provides powerful Excel manipulation capabilities.
- Standard Python Libraries → xml.etree.ElementTree, csv, and re.
You can install Spire.XLS via pip (assuming you have Python and pip installed on your system):
pip install spire.xls
Additionally, make sure you have a CSV file ready to test. A simple one might look like this:
ID,FirstName,LastName,Age,Department,Salary
1,John,Doe,30,Sales,5000
2,Jane,Smith,28,Marketing,4500
3,Mike,Johnson,35,IT,6000
4,Emily,Brown,32,HR,4800
Convert CSV to Excel XML Format in Python
The first approach is to convert CSV into Excel-compatible XML . This format is sometimes called SpreadsheetML , introduced in Excel 2003. It allows CSV data to be transformed into a fully structured XML file that Excel can open directly.
With Spire.XLS, this process is straightforward:
from spire.xls import *
# Create a Workbook
workbook = Workbook()
# Load CSV into the first worksheet
workbook.LoadFromFile("input.csv", ",", 1, 1)
# Save as XML Spreadsheet format
workbook.SaveAsXml("output.xml")
# Dispose resources
workbook.Dispose()
How it Works
- LoadFromFile() – Reads the CSV file into the workbook.
- SaveAsXml() – Saves it as SpreadsheetML XML format.
This approach is best if your end goal is Excel compatibility rather than pure XML processing.
Output:

You may also like: Convert Excel to Excel-Compatible XML in Python
Convert CSV to Standard XML in Python
Sometimes, you need a custom XML format rather than Excel-compatible XML. For instance, you may need an XML structure like:
<Employees>
<Employee>
<ID>1</ID>
<FirstName>John</FirstName>
<LastName>Doe</LastName>
<Department>Sales</Department>
<Salary>5000</Salary>
</Employee>
</Employees>
Here’s how you can achieve that:
from spire.xls import *
import xml.etree.ElementTree as ET
# Step 1: Load CSV into workbook
workbook = Workbook()
workbook.LoadFromFile("input.csv", ",", 1, 1)
sheet = workbook.Worksheets[0]
# Step 2: Create root element
root = ET.Element("Root")
# Step 3: Loop through rows and build XML
# Assume first row is header
headers = []
for col in range(1, sheet.Columns.Count + 1):
cell_value = sheet.Range[1, col].Value
if not cell_value:
break
headers.append(cell_value)
# Add data rows
for row in range(2, sheet.Rows.Count + 1):
# Stop if row is empty
if not sheet.Range[row, 1].Value:
break
record = ET.SubElement(root, "Record")
for col, header in enumerate(headers, start=1):
cell_value = sheet.Range[row, col].Value
field = ET.SubElement(record, header)
field.text = cell_value if cell_value else ""
# Step 4: Save to XML file
tree = ET.ElementTree(root)
tree.write("output.xml", encoding="utf-8", xml_declaration=True)
# Cleanup
workbook.Dispose()
Explanation
- Step 1: Load the CSV into a worksheet.
- Step 2: Create a root <Root> element for the XML tree.
- Step 3: Extract headers from the first row and use them as XML tags.
- Step 4: Loop through rows and add <Record> elements with child fields.
- Step 5: Save the XML to a file.
This approach is flexible because you can customize the XML schema however you like (e.g., change <Record> to <Employee>).
Output:

You may also like: How to Convert XML to CSV in Python
Handle Real-World CSV Challenges
Converting a “perfect” CSV to XML is easy. But real-world CSVs are rarely perfect. Let’s discuss common issues developers face and how to fix them.
-
Headers with Invalid XML Names
- Problem: CSV headers like Employee ID or 123Name are invalid XML tags since XML element names cannot have spaces, start with numbers, or include special characters.
- Fix: Clean headers by replacing spaces with underscores (_) or prefixing numeric headers with a safe string.
-
Empty or Missing Values
- Problem: Real-world CSVs often have missing cells. When converted to XML, missing cells may create malformed structures.
- Fix: Replace blanks with placeholders (NULL, Unknown, or 0).
-
Special Characters in Data
- Problem: Characters like <, >, & inside CSV values can break XML if not escaped.
- Fix: Always escape XML entities properly (<, >, &).
-
Nested / Hierarchical Data in Flat CSV
- Problem: Some CSV cells contain multiple values or hierarchical information (e.g., lists of products) within a single field. Converting directly to XML will flatten the data, losing the intended hierarchy.
Example:
OrderID,Customer,Products
1001,John,"Laptop;Mouse;Keyboard"
Flattened XML (bad):
<Products>Laptop;Mouse;Keyboard</Products>
- Fix: Detect list-like or nested values and expand them into child XML elements or multiple rows, preserving the structure in XML.
Hierarchical XML (fixed):
<Products>
<Product>Laptop</Product>
<Product>Mouse</Product>
<Product>Keyboard</Product>
</Products>
Automating Fixes with clean_csv
Instead of cleaning data manually, you can automate preprocessing with the following helper function:
import csv
import re
def clean_csv(input_file, output_file, nested_columns=None, nested_delimiter=";"):
if nested_columns is None:
nested_columns = []
cleaned_rows = []
# Escape special characters for XML
def escape_xml(text):
return (text.replace("&", "&")
.replace("<", "<")
.replace(">", ">")
.replace('"', """)
.replace("'", "'"))
with open(input_file, "r", encoding="utf-8") as infile:
reader = csv.reader(infile)
headers = next(reader)
# Clean headers
cleaned_headers = []
for h in headers:
h = h.strip()
h = re.sub(r"\s+", "_", h) # Replace spaces with _
h = re.sub(r"[^a-zA-Z0-9_]", "", h) # Remove invalid chars
if re.match(r"^\d", h): # Cannot start with digit
h = "Field_" + h
cleaned_headers.append(h)
cleaned_rows.append(cleaned_headers)
# Read all rows first
raw_rows = []
for row in reader:
# Handle empty cells
row = [cell if cell.strip() != "" else "NULL" for cell in row]
raw_rows.append(row)
# Expand nested columns
if nested_columns:
expanded_rows = [cleaned_headers] # include header
for row in raw_rows:
row_variants = [row]
for col_name in nested_columns:
if col_name not in cleaned_headers:
continue
col_index = cleaned_headers.index(col_name)
temp = []
for variant in row_variants:
cell_value = variant[col_index]
# Only split on the nested delimiter, not on XML special chars
if nested_delimiter in cell_value:
items = [item.strip() for item in cell_value.split(nested_delimiter)]
for item in items:
new_variant = variant.copy()
new_variant[col_index] = item
temp.append(new_variant)
else:
temp.append(variant)
row_variants = temp
expanded_rows.extend(row_variants)
cleaned_rows = expanded_rows
else:
cleaned_rows.extend(raw_rows)
# Escape special characters after expansion
final_rows = [cleaned_rows[0]] # header
for row in cleaned_rows[1:]:
final_row = [escape_xml(cell) for cell in row]
final_rows.append(final_row)
# Write cleaned CSV
with open(output_file, "w", newline="", encoding="utf-8") as outfile:
writer = csv.writer(outfile)
writer.writerows(final_rows)
print(f"Cleaned CSV saved to {output_file}")
You can invoke the clean_csv function by passing the input CSV file path, output CSV file path, and optionally the columns to expand for nested values.
# File paths
input_file = r"C:\Users\Administrator\Desktop\input.csv"
output_file = r"C:\Users\Administrator\Desktop\cleaned_output.csv"
# Specify columns that may contain nested values
nested_columns = ["Products"] # you can add more, e.g., ["Products", "Reviews"]
# Call the clean_csv function
clean_csv(input_file, output_file, nested_columns=nested_columns, nested_delimiter=";")
This function ensures your CSV is safe and clean before converting it to XML.
It helps:
- Clean headers (valid XML names)
- Handle empty cells
- Expand nested values in any specified column(s)
- Escape special characters for XML
- Write a clean, UTF-8 CSV ready for XML conversion
Wrap Up
Converting CSV to XML in Python with Spire.XLS is both powerful and flexible. If you only need Excel-readable XML, saving directly to SpreadsheetML is the quickest option. If you need custom XML structures for APIs, migrations, or reporting, you can build standard XML using xml.etree.ElementTree.
Real-world CSVs are messy. They may have missing values, invalid headers, special characters, or nested lists. By integrating a preprocessing step (like the clean_csv function), you ensure that your XML output is both valid and meaningful.
Whether you’re preparing data for enterprise integration, creating structured reports, or exchanging information with legacy systems, this workflow gives you the best of both worlds: the simplicity of CSV and the structure of XML.
FAQs
Q1: Can I convert very large CSV files to XML?
Yes, but for very large files, you should use a streaming approach (processing rows one by one) to avoid memory issues.
Q2: Does Spire.XLS support direct conversion to XML without custom code?
Yes. Saving as SpreadsheetML is built-in. But for custom XML, you’ll need to write Python code as shown.
Q3: How do I handle special characters automatically?
Use the escape_xml helper or Python’s built-in xml.sax.saxutils.escape().
Q4: What if my CSV has nested data in multiple columns?
You can pass multiple column names into nested_columns when calling clean_csv.
Q5: Can I validate the generated XML?
Yes. After generating XML, validate it against an XSD schema if your system requires strict formatting.
Get a Free License
To fully experience the capabilities of Spire.XLS for Python without any evaluation limitations, you can request a free 30-day trial license.

XML is a widely used format for storing structured data, but it’s not ideal for analysis or tabular tools like Excel. Converting XML to CSV allows you to work with the data in a simpler, flat format that’s compatible with spreadsheets and data analysis libraries. By converting XML to CSV, you can easily import data into Excel, perform calculations, or feed it into Python data analysis tools like Pandas.
This approach also helps standardize complex hierarchical data into a format that is easier to read, manipulate, and share across different applications. In this tutorial, we’ll explore how to efficiently transform XML files into CSV using Spire.XLS for Python.
Table of Contents:
- Setting Up Spire.XLS for Python
- Understanding XML Data Structures
- How XML Data Is Extracted and Converted
- Basic Example: Convert XML to CSV with Python
- Advanced Techniques
- Troubleshooting and Common Issues
- Conclusion
- FAQs
Setting Up Spire.XLS for Python
Before we can start, we need to install the Spire.XLS library. The package is available on PyPI , so installation is straightforward. Run the following command:
pip install spire.xls
Once installed, you can import the required classes:
from spire.xls import *
from spire.xls.common import *
Spire.XLS provides the Workbook and Worksheet objects for managing Excel-like files. You’ll use them to create new CSV files and populate them with your XML data.
Understanding XML Data Structures
XML files are organized in a tree-like hierarchy, where elements (or nodes) are enclosed in tags. Each element can contain text, attributes, or even other child elements.
For example, consider this Books.xml:
<catalog>
<book isbn="9780451524935">
<title>The Catcher in the Rye</title>
<author>J.D. Salinger</author>
<genre>Fiction</genre>
<reviews>
<review>Excellent coming-of-age story</review>
<review>A true classic</review>
</reviews>
</book>
<book isbn="9780439023528">
<title>The Hunger Games</title>
<author>Suzanne Collins</author>
<genre>Adventure</genre>
</book>
</catalog>
- Root node : <catalog> is the top-level container.
- Child nodes : Each <book> is a child of <catalog>.
- Elements : <title>, <author>, and <genre> are elements inside each <book>.
- Attributes : The isbn in <book isbn="..."> is an attribute attached to the book element.
- Nested elements : The <reviews> node contains multiple <review> sub-nodes.
Challenges when converting XML to CSV:
- Hierarchical data – XML allows nesting (e.g., <reviews> inside <book>), while CSV is flat.
- Attributes vs. elements – Data may be stored as an attribute (isbn) or as a tag (title).
- Optional fields – Not all <book> elements may contain the same tags, which can lead to missing values in the CSV.
A robust converter must be able to handle these differences and map hierarchical XML into a flat, tabular CSV format.
How XML Data Is Extracted and Converted
To load and parse an XML file in Python, you can use the built-in xml.etree.ElementTree library. This library lets you navigate the XML tree, retrieve elements, and access attributes. For example:
import xml.etree.ElementTree as ET
# Load the XML file
tree = ET.parse("Books.xml")
root = tree.getroot()
# Iterate through elements
for book in root.findall("book"):
title = book.findtext("title", "")
author = book.findtext("author", "")
isbn = book.attrib.get("isbn", "")
After extracting the XML data, the next step is to map it to a tabular structure . Using Spire.XLS for Python , you can create a workbook, write the extracted values into worksheet cells, and finally export the worksheet as a CSV file for easy analysis.
General Steps to Convert XML to CSV in Python
- Use xml.etree.ElementTree to load and retrieve data from the XML file.
- Create a Workbook object using Spire.XLS.
- Add a worksheet with Workbook.Worksheets.Add() .
- Write extracted XML data into the worksheet using Worksheet.SetValue() .
- Save the worksheet to a CSV file using Worksheet.SaveToFile() .
Basic Example: Convert XML to CSV with Python
Let’s start with a basic XML-to-CSV conversion. This example automatically generates headers by inspecting the first <book> element and then exports all child nodes into CSV.
from spire.xls import *
from spire.xls.common import *
import xml.etree.ElementTree as ET
# Create a Workbook object
workbook = Workbook()
# Remove default worksheets
workbook.Worksheets.Clear()
# Add a worksheet and name it
worksheet = workbook.Worksheets.Add("Books")
# Load an XML file
xml_tree = ET.parse("C:\\Users\\Administrator\\Desktop\\Books.xml")
# Get the root element of the XML tree
xml_root = xml_tree.getroot()
# Get the first the "book" element
first_book = xml_root.find("book")
# Extract header information and convert it into a list
header = list(first_book.iter())[1:]
# Write header to Excel
for col_index, header_node in enumerate(header, start=1):
header_text = header_node.tag
worksheet.SetValue(1, col_index, header_text)
# Write other data to Excel by iterating over each book element and each data node within it
row_index = 2
for book in xml_root.iter("book"):
for col_index, data_node in enumerate(list(book.iter())[1:], start=1):
value = data_node.text
header_text = list(header[col_index - 1].iter())[0].tag
worksheet.SetValue(row_index, col_index, value)
row_index += 1
# Save the document to an Excel file
worksheet.SaveToFile("output/XmlToCsv.csv", ",", Encoding.get_UTF8())
# Dispose resources
workbook.Dispose()
This script works well for simple, flat XML structures. It automatically generates headers (title, author, genre, price) and fills each row with corresponding values.
Output:

You might also be interested in: Convert XML to Excel and PDF in Python
Advanced Techniques
The basic script works in many cases, but XML often isn’t so simple. Let’s go through some advanced techniques to handle real-world scenarios.
Exporting Only Specific Elements
Sometimes your XML has more data than you need. Maybe you only want to export title and author, ignoring everything else.
from spire.xls import *
from spire.xls.common import *
import xml.etree.ElementTree as ET
# Create a Workbook object
workbook = Workbook()
# Remove default worksheets
workbook.Worksheets.Clear()
# Add a worksheet and name it
worksheet = workbook.Worksheets.Add("Books")
# Load the XML file
xml_tree = ET.parse(r"C:\Users\Administrator\Desktop\Books.xml")
xml_root = xml_tree.getroot()
# Define the elements you want to export
selected_elements = ["title", "author"]
# Write header
for col_index, tag in enumerate(selected_elements, start=1):
worksheet.SetValue(1, col_index, tag)
# Write data
row_index = 2
for book in xml_root.iter("book"):
for col_index, tag in enumerate(selected_elements, start=1):
# Use findtext to handle missing values safely
worksheet.SetValue(row_index, col_index, book.findtext(tag, ""))
row_index += 1
# Save the document to a CSV file
worksheet.SaveToFile("output/XmlToCsv_Selected.csv", ",", Encoding.get_UTF8())
# Dispose resources
workbook.Dispose()
This approach ensures your CSV only contains the columns you care about.
Output:

Including XML Attributes in CSV
What if your XML contains important data stored as attributes, such as isbn? You can easily include them:
from spire.xls import *
from spire.xls.common import *
import xml.etree.ElementTree as ET
# Create a Workbook object
workbook = Workbook()
# Remove default worksheets
workbook.Worksheets.Clear()
# Add a worksheet and name it
worksheet = workbook.Worksheets.Add("Books")
# Load an XML file
xml_tree = ET.parse(r"C:\Users\Administrator\Desktop\Books.xml")
# Get the root element of the XML tree
xml_root = xml_tree.getroot()
# Get the first the "book" element
first_book = xml_root.find("book")
# Extract header information (child nodes)
header = list(first_book.iter())[1:]
# Write header to Excel
worksheet.SetValue(1, 1, "isbn") # <-- Add ISBN column first
for col_index, header_node in enumerate(header, start=2): # start at 2 now
header_text = header_node.tag
worksheet.SetValue(1, col_index, header_text)
# Write data
row_index = 2
for book in xml_root.iter("book"):
# Write isbn as text
isbn_value = book.attrib.get("isbn", "")
worksheet.Range[row_index, 1].Text = isbn_value
# Then write other fields
for col_index, data_node in enumerate(list(book.iter())[1:], start=2):
value = data_node.text
worksheet.SetValue(row_index, col_index, value)
row_index += 1
# Format the whole ISBN column as text to prevent scientific notation
last_row = row_index - 1
isbn_range = f"A2:A{last_row}"
worksheet.Range[isbn_range].NumberFormat = "@"
# Save the document to an Excel file (CSV format)
worksheet.SaveToFile("output/XmlToCsv_WithAttributes.csv", ",", Encoding.get_UTF8())
# Dispose resources
workbook.Dispose()
Here we explicitly create an ISBN column, extract it from each <book>’s attributes, and format it as text to prevent Excel from displaying it in scientific notation.
Output:

Handling Nested XML Structures
Nested nodes are common. Suppose your XML looks like this:
<catalog>
<book>
<title>1984</title>
<author>George Orwell</author>
<reviews>
<review>Excellent read!</review>
<review>Still relevant today.</review>
</reviews>
</book>
</catalog>
Here’s how to collapse multiple <review> entries into a single CSV column:
from spire.xls import *
from spire.xls.common import *
import xml.etree.ElementTree as ET
# Create a Workbook object
workbook = Workbook()
# Remove default worksheets
workbook.Worksheets.Clear()
# Add a worksheet and name it
worksheet = workbook.Worksheets.Add("Books")
# Load an XML file
xml_tree = ET.parse(r"C:\Users\Administrator\Desktop\Nested.xml")
xml_root = xml_tree.getroot()
# Get the first <book> element
first_book = xml_root.find("book")
# Collect headers (auto-detected)
header = []
for child in first_book:
if child.tag == "reviews":
header.append("reviews") # collapse nested <review> into one column
else:
header.append(child.tag)
# Write header row
for col_index, header_text in enumerate(header, start=1):
worksheet.SetValue(1, col_index, header_text)
# Write data rows
row_index = 2
for book in xml_root.iter("book"):
col_index = 1
for child in book:
if child.tag == "reviews":
# Join all <review> texts into a single cell
reviews = [r.text for r in child.findall("review") if r.text]
worksheet.SetValue(row_index, col_index, "; ".join(reviews))
else:
worksheet.SetValue(row_index, col_index, child.text if child.text else "")
col_index += 1
row_index += 1
# Save to CSV
worksheet.SaveToFile("output/XmlToCsv_WithReviews.csv", ",", Encoding.get_UTF8())
# Dispose resources
workbook.Dispose()
Output:

Spire.XLS not only supports importing data from standard XML files into Excel or CSV, but also allows converting OpenXML (Microsoft's XML-based file format) to Excel. If you're interested, check out this tutorial: How to Convert Excel to OpenXML and OpenXML to Excel in Python.
Troubleshooting and Common Issues
Even with a well-structured script, you may encounter some common issues when converting XML to CSV:
- Scientific notation in Excel
- Problem: Long numeric strings like ISBNs may appear as 9.78045E+12 instead of the full number.
- Solution: Format the column as text before saving, for example:
worksheet.Range["A2:A{last_row}"].NumberFormat = "@" -
Missing values causing errors
- Problem: Some <book> elements may lack optional fields (e.g., <genre>). Attempting to access .text directly can cause errors.
- Solution: Use findtext(tag, "") to safely provide a default empty string.
-
Incomplete or unexpected headers
- Problem: If you generate headers from only the first <book>, you might miss fields that appear later in the XML.
- Solution: Scan multiple elements (or the entire dataset) to build a complete header list before writing data.
-
Encoding issues
- Problem: Special characters (such as accents or symbols) may not display correctly in the CSV.
- Solution: Always save with UTF-8 encoding:
worksheet.SaveToFile("output.csv",",", Encoding.get_UTF8())
Conclusion
Converting XML to CSV in Python doesn’t have to be painful. With Spire.XLS for Python, you can automate much of the process, including header generation, handling attributes, and flattening nested nodes. Whether you’re exporting only a few fields, working with complex hierarchies, or cleaning up messy XML, Spire.XLS gives you the flexibility to handle it.
By integrating these approaches into your workflow, you can turn structured XML datasets into clean, analysis-ready CSV files in just a few lines of code.
FAQs
Q1: Can I export directly to Excel (.xlsx) instead of CSV?
Yes. Simply use workbook.SaveToFile("output.xlsx", ExcelVersion.Version2016).
Q2: How do I handle very large XML files?
Use Python’s iterparse() from xml.etree.ElementTree to stream large files instead of loading them fully into memory.
Q3: What if some <book> elements contain additional tags?
You can enhance the header-building step to scan all <book> nodes and collect unique tags dynamically.
Q4: Can I customize the CSV delimiter (e.g., use ; instead of ,)?
Yes. When calling SaveToFile(), replace the delimiter argument:
worksheet.SaveToFile("output.csv", ";", Encoding.get_UTF8())
Q5: How do I export nested XML structures (e.g., multiple <review> nodes)?
Flatten them into a single cell by joining values. For example:
reviews = [r.text for r in book.find("reviews").findall("review")]
worksheet.SetValue(row_index, col_index, "; ".join(reviews))
Get a Free License
To fully experience the capabilities of Spire.XLS for Python without any evaluation limitations, you can request a free 30-day trial license.
JSON to CSV in Python – Complete Guide for Flat & Nested JSON
2025-09-11 09:05:57 Written by zaki zou
JSON (JavaScript Object Notation) is the most widely used format for structured data in web applications, APIs, and configuration files. CSV (Comma-Separated Values) is a popular format for data analysis, reporting, and seamless integration with Excel. Converting JSON to CSV is a common requirement for developers, data engineers, and analysts who need to transform hierarchical JSON data into a tabular format for easier processing, visualization, and automation.
This step-by-step guide demonstrates how to convert JSON to CSV using Python, including techniques for handling flat and nested JSON data, and optimizing performance for large JSON datasets. By following this tutorial, you can streamline your data workflows and efficiently integrate JSON data into Excel or other tabular systems.
What You Will Learn
- Why Convert JSON to CSV
- Understanding JSON Structures
- Python JSON to CSV Converter – Installation
- Step-by-Step JSON to CSV Conversion in Python
- Performance Tips for Exporting Large or Complex JSON Files to CSV
- Conclusion
- FAQs
Why Convert JSON to CSV?
Although JSON is ideal for hierarchical and complex data, CSV offers several practical advantages:
- Spreadsheet Compatibility: CSV files can be opened in Excel, Google Sheets, and other BI tools.
- Ease of Analysis: Tabular data is easier to filter, sort, summarize, and visualize.
- Data Pipeline Integration: Many ETL workflows and reporting systems rely on CSV for seamless data integration.
Real-World Use Cases:
- API Data Extraction: Transform JSON responses from web APIs into CSV for analysis or reporting.
- Reporting Automation: Convert application or system logs into CSV for automated dashboards or scheduled reports.
- Data Analytics: Prepare hierarchical JSON data for Excel, Google Sheets, or BI tools like Tableau to perform pivot tables and visualizations.
- ETL Pipelines: Flatten and export JSON from databases or log files into CSV for batch processing or integration into data warehouses.
Converting JSON files to CSV format bridges the gap between structured storage and table-based analytics, making it a common requirement in reporting, data migration, and analytics pipelines.
Understanding JSON Structures
Before implementing JSON to CSV conversion in Python, it is important to understand the two common structures of JSON data:
- Flat JSON: Each object contains simple key-value pairs.
[
{"name": "Alice", "age": 28, "city": "New York"},
{"name": "Bob", "age": 34, "city": "Los Angeles"}
]
- Nested JSON: Objects contain nested dictionaries or arrays.
[
{
"name": "Alice",
"age": 28,
"contacts": {"email": "alice@example.com", "phone": "123-456"}
}
]
Flat JSON can be directly mapped to CSV columns, while nested JSON requires flattening to ensure proper tabular representation.
Python JSON to CSV Converter - Installation
To export JSON to CSV in Python, you can use Spire.XLS for Python, a spreadsheet library that enables Python developers to create, read, manipulate, and export spreadsheet files directly from Python. It supports common formats like .xls, .xlsx, .csv, and .ods.
Installation
You can install the library from PyPI via pip:
pip install spire.xls
If you need assistance with the installation, refer to this tutorial: How to Install Spire.XLS for Python on Windows.
Once installed, you can import it into your Python scripts:
from spire.xls import *
This setup allows seamless JSON-to-CSV conversion with complete control over the workbook structure and output format.
Step-by-Step JSON to CSV Conversion in Python
Converting JSON to CSV involves four main steps: loading JSON data, creating a workbook, writing headers and rows, and exporting the final file as CSV. Below, we’ll go through the process for both flat JSON and nested JSON.
Handling Flat JSON Data
Step 1: Load JSON and Create Workbook
First, load your JSON file into Python and create a new workbook where the data will be written.
import json
from spire.xls import *
# Load JSON data
with open('data.json') as f:
data = json.load(f)
# Create workbook and worksheet
workbook = Workbook()
sheet = workbook.Worksheets[0]
Explanation:
- json.load parses your JSON file into Python objects (lists and dictionaries).
- Workbook() creates a new Excel workbook in memory.
- workbook.Worksheets[0] accesses the first worksheet where data will be written.
Step 2: Write Headers Dynamically
Next, generate column headers from the JSON keys. This ensures that the CSV reflects the structure of your data.
# Extract headers from JSON keys
headers = list(data[0].keys())
# Write headers to the first row
for col, header in enumerate(headers, start=1):
sheet.Range[1, col].Value = header
Explanation:
- list(data[0].keys()) retrieves all top-level JSON keys.
- sheet.Range[1, col].Value writes headers in the first row, starting from column 1.
Step 3: Write Data Rows
After headers are set, populate the worksheet row by row with values from each JSON object.
# Populate values from each JSON object to the subsequent rows
for row_index, item in enumerate(data, start=2):
for col_index, key in enumerate(headers, start=1):
value = item.get(key, "")
sheet.Range[row_index, col_index].Value = str(value) if value is not None else ""
Explanation:
- Loop starts from row 2 because row 1 is reserved for headers.
- Each JSON object is mapped to a row, and each key is mapped to a column.
Step 4: Save the Worksheet as CSV
Finally, save the worksheet as a CSV file and clean up resources.
# Save the worksheet as a CSV file
sheet.SaveToFile("output.csv", ",", Encoding.get_UTF8())
workbook.Dispose()
Resulting CSV (output.csv):

This example uses Python’s built-in json module to parse JSON data. For more details on its functions and usage, refer to the Python json module documentation.
If you also want to implement JSON to Excel conversion, see our guide on converting JSON to/from Excel in Python.
Handling Nested JSON with Dictionaries and Arrays
When JSON objects contain nested dictionaries or arrays, direct CSV export is not possible because CSV is a flat format. To ensure compatibility, nested fields must be flattened to preserve all data in a readable CSV format.
Suppose you have the following JSON file (nested.json) with nested data:
[
{
"Name": "Alice",
"Age": 28,
"City": "New York",
"Contacts": {"Email": "alice@example.com", "Phone": "123-456"},
"Skills": ["Python", "Excel", "SQL"]
},
{
"Name": "Bob",
"Age": 34,
"City": "Los Angeles",
"Contacts": {"Email": "bob@example.com", "Phone": "987-654"},
"Skills": ["Java", "CSV", "JSON"]
}
]
This JSON contains:
- Flat fields: Name, Age, City
- Nested dictionary: Contacts
- Array: Skills
To export all fields to CSV, nested dictionaries need to be flattened, and arrays need to be joined into semicolon-separated strings. The following steps show you how to achieve this.
Step 1: Flatten Nested JSON Data
Flattening nested JSON with dictionaries and arrays involves converting nested dictionaries into dot-notation keys and joining array values into a single string. The following helper function performs this flattening:
import json
from spire.xls import *
# ---------------------------
# Step 1: Flatten nested JSON
# ---------------------------
def flatten_json(item):
flat = {}
for key, value in item.items():
if isinstance(value, dict):
for sub_key, sub_value in value.items():
flat[f"{key}.{sub_key}"] = sub_value
elif isinstance(value, list):
flat[key] = "; ".join(map(str, value))
else:
flat[key] = value
return flat
with open('nested.json') as f:
data = json.load(f)
flat_data = [flatten_json(item) for item in data]
Explanation:
- flatten_json() checks each field in the JSON object.
- If the value is a dictionary, its keys are prefixed with the parent key (dot notation).
- If the value is a list, all elements are joined into one string separated by semicolons.
- Other values are kept as they are.
- Finally, the original nested JSON is transformed into flat_data, which can now be exported using the same workflow as flat JSON.
Step 2: Export Flattened Data to CSV
After flattening, the export process is the same as flat JSON: create a workbook, generate headers, write rows, and save as CSV.
# ------------------------------------------------
# Step 2: Export flattened data to CSV
# (create workbook, write headers, populate rows, save)
# ------------------------------------------------
workbook = Workbook()
sheet = workbook.Worksheets[0]
# Generate headers from flattened JSON keys
headers = list(flat_data[0].keys())
for col, header in enumerate(headers, start=1):
sheet.Range[1, col].Value = header
# Populate rows from flat_data
for row_index, item in enumerate(flat_data, start=2):
for col_index, key in enumerate(headers, start=1):
value = item.get(key, "")
sheet.Range[row_index, col_index].Value = str(value) if value is not None else ""
# Save the worksheet as CSV (comma delimiter, UTF-8)
sheet.SaveToFile("output.csv", ",", Encoding.get_UTF8())
# Clean up resources
workbook.Dispose()
Explanation:
- Headers and rows are generated from flat_data (instead of the original data).
- This ensures nested fields (Contacts.Email, Contacts.Phone) and arrays (Skills) are properly included in the CSV.
Resulting CSV (output.csv):

Performance Tips for Exporting Large or Complex JSON Files to CSV
When working with large or complex JSON files, applying a few practical strategies can help optimize memory usage, improve performance, and ensure your CSV output remains clean and accurate.
- For very large datasets, process JSON in chunks to avoid memory issues.
- When arrays are very long, consider splitting into multiple rows instead of joining with semicolons.
- Maintain consistent JSON keys for cleaner CSV output.
- Test the CSV output in Excel to verify formatting, especially for dates and numeric values.
Conclusion
Converting JSON to CSV in Python is essential for developers and data engineers. With Spire.XLS for Python, you can:
- Convert flat and nested JSON into structured CSV.
- Maintain consistent headers and data formatting.
- Handle large datasets efficiently.
- Export directly to CSV or Excel without additional dependencies.
By following this guide, you can seamlessly transform JSON into CSV for reporting, analytics, and integration.
FAQs
Q1: Can Spire.XLS for Python convert nested JSON objects to CSV?
A1: Yes. You can flatten nested JSON objects, including dictionaries and arrays, and export them as readable CSV columns with dynamic headers. This ensures all hierarchical data is preserved in a tabular format.
Q2: How do I install Spire.XLS for Python?
A2: You can install it via pip with the command:
pip install spire.xls
Then import it into your Python script using:
from spire.xls import
This setup enables seamless JSON-to-CSV conversion in Python.
Q3: Can I join array elements from JSON into a single CSV cell?
A3: Yes. You can join array elements from JSON into a single CSV cell using a delimiter like ;. This keeps multiple values readable and consistent in your exported CSV file.
Q4: How can I optimize performance when converting large JSON files to CSV in Python?
A4: To handle large JSON files efficiently: flatten nested JSON before writing, process data row-by-row, and export in batches. This minimizes memory usage and ensures smooth CSV generation.
Q5: Can I customize the CSV delimiter or column order when exporting JSON to CSV?
A5: Yes. Spire.XLS allows you to set custom delimiters (e.g., comma, semicolon, or tab) and manually define the column order, giving you full control over professional CSV output.
Convert JSON to/from Excel in Python – Full Guide with Examples
2025-07-16 05:39:52 Written by zaki zou
In many Python projects, especially those that involve APIs, data analysis, or business reporting, developers often need to convert Excel to JSON or JSON to Excel using Python code. These formats serve different but complementary roles: JSON is ideal for structured data exchange and storage, while Excel is widely used for sharing, editing, and presenting data in business environments.
This tutorial provides a complete, developer-focused guide to converting between JSON and Excel in Python. You'll learn how to handle nested data, apply Excel formatting, and resolve common conversion or encoding issues. We’ll use Python’s built-in json module to handle JSON data, and Spire.XLS for Python to read and write Excel files in .xlsx, .xls, and .csv formats — all without requiring Microsoft Excel or other third-party software.
Topics covered include:
- Install Spire.XLS for Python
- Why Choose Spire.XLS over General-Purpose Libraries?
- Convert JSON to Excel in Python
- Convert Excel to JSON in Python
- Real-World Example: Handling Nested JSON and Complex Excel Formats
- Common Errors and Fixes
- FAQ
- Conclusion
Install Spire.XLS for Python
This library is used in this tutorial to generate and parse Excel files (.xlsx, .xls, .csv) as part of the JSON–Excel conversion workflow.
To get started, install the Spire.XLS for Python package from PyPI:
pip install spire.xls
You can also choose Free Spire.XLS for Python in smaller projects:
pip install spire.xls.free
Spire.XLS for Python runs on Windows, Linux, and macOS. It does not require Microsoft Excel or any COM components to be installed.
Why Choose Spire.XLS over Open-Source Libraries?
Many open-source Python libraries are great for general Excel tasks like simple data export or basic formatting. If your use case only needs straightforward table output, these tools often get the job done quickly.
However, when your project requires rich Excel formatting, multi-sheet reports, or an independent solution without Microsoft Office, Spire.XLS for Python stands out by offering a complete Excel feature set.
| Capability | Open-Source Libraries | Spire.XLS for Python |
|---|---|---|
| Advanced Excel formatting | Basic styling | Full styling API for reports |
| No Office/COM dependency | Fully standalone | Fully standalone |
| Supports .xls, .xlsx, .csv | .xlsx and .csv mostly; .xls may need extra packages | Full support for .xls, .xlsx, .csv |
| Charts, images, shapes | Limited or none | Built-in full support |
For developer teams that need polished Excel files — with complex layouts, visuals, or business-ready styling — Spire.XLS is an efficient, all-in-one alternative.
Convert JSON to Excel in Python
In this section, we’ll walk through how to convert structured JSON data into an Excel file using Python. This is especially useful when exporting API responses or internal data into .xlsx reports for business users or analysts.
Step 1: Prepare JSON Data
We’ll start with a JSON list of employee records:
[
{"employee_id": "E001", "name": "Jane Doe", "department": "HR"},
{"employee_id": "E002", "name": "Michael Smith", "department": "IT"},
{"employee_id": "E003", "name": "Sara Lin", "department": "Finance"}
]
This is a typical structure returned by APIs or stored in log files. For more complex nested structures, see the real-world example section.
Step 2: Convert JSON to Excel in Python with Spire.XLS
from spire.xls import Workbook, FileFormat
import json
# Load JSON data from file
with open("employees.json", "r", encoding="utf-8") as f:
data = json.load(f)
# Create a new Excel workbook and access the first worksheet
workbook = Workbook()
sheet = workbook.Worksheets[0]
# Write headers to the first row
headers = list(data[0].keys())
for col, header in enumerate(headers):
sheet.Range[1, col + 1].Text = header
# Write data rows starting from the second row
for row_idx, row in enumerate(data, start=2):
for col_idx, key in enumerate(headers):
sheet.Range[row_idx, col_idx + 1].Text = str(row.get(key, ""))
# Auto-fit the width of all used columns
for i in range(1, sheet.Range.ColumnCount + 1):
sheet.AutoFitColumn(i)
# Save the Excel file and release resources
workbook.SaveToFile("output/employees.xlsx", FileFormat.Version2016)
workbook.Dispose()
Code Explanation:
- Workbook() initializes the Excel file with three default worksheets.
- workbook.Worksheets[] accesses the specified worksheet.
- sheet.Range(row, col).Text writes string data to a specific cell (1-indexed).
- The first row contains column headers based on JSON keys, and each JSON object is written to a new row beneath it.
- workbook.SaveToFile() saves the Excel workbook to disk. You can specify the format using the FileFormat enum — for example, Version97to2003 saves as .xls, Version2007 and newer save as .xlsx, and CSV saves as .csv.
The generated Excel file (employees.xlsx) with columns employee_id, name, and department.

You can also convert the Excel worksheet to a CSV file using Spire.XLS for Python if you need a plain text output format.
Convert Excel to JSON in Python
This part explains how to convert Excel data back into structured JSON using Python. This is a common need when importing .xlsx files into web apps, APIs, or data pipelines that expect JSON input.
Step 1: Load the Excel File
First, we use Workbook.LoadFromFile() to load the Excel file, and then select the worksheet using workbook.Worksheets[0]. This gives us access to the data we want to convert into JSON format.
from spire.xls import Workbook
# Load the Excel file
workbook = Workbook()
workbook.LoadFromFile("products.xlsx")
sheet = workbook.Worksheets[0]
Step 2: Extract Excel Data and Write to JSON
import json
# Get the index of the last row and column
rows = sheet.LastRow
cols = sheet.LastColumn
# Extract headers from the first row
headers = [sheet.Range[1, i + 1].Text for i in range(cols)]
data = []
# Map each row to a dictionary using headers
for r in range(2, rows + 1):
row_data = {}
for c in range(cols):
value = sheet.Range[r, c + 1].Text
row_data[headers[c]] = value
data.append(row_data)
# Write JSON output
with open("products_out.json", "w", encoding="utf-8") as f:
json.dump(data, f, indent=2, ensure_ascii=False)
Code Explanation:
- sheet.LastRow and sheet.LastColumn detect actual used cell range.
- The first row is used to extract field names (headers).
- Each row is mapped to a dictionary, forming a list of JSON objects.
- sheet.Range[row, col].Text returns the cell’s displayed text, including any formatting (like date formats or currency symbols). If you need the raw numeric value or a real date object, you can use .Value, .NumberValue, or .DateTimeValue instead.
The JSON file generated from the Excel data using Python:

If you’re not yet familiar with how to read Excel files in Python, see our full guide: How to Read Excel Files in Python Using Spire.XLS.
Real-World Example: Handling Nested JSON and Formatting Excel
In real-world Python applications, JSON data often contains nested dictionaries or lists, such as contact details, configuration groups, or progress logs. At the same time, the Excel output is expected to follow a clean, readable layout suitable for business or reporting purposes.
In this section, we'll demonstrate how to flatten nested JSON data and format the resulting Excel sheet using Python and Spire.XLS. This includes merging cells, applying styles, and auto-fitting columns — all features that help present complex data in a clear tabular form.
Let’s walk through the process using a sample file: projects_nested.json.
Step 1: Flatten Nested JSON
Sample JSON file (projects_nested.json):
[
{
"project_id": "PRJ001",
"title": "AI Research",
"manager": {
"name": "Dr. Emily Wang",
"email": "emily@lab.org"
},
"phases": [
{"phase": "Design", "status": "Completed"},
{"phase": "Development", "status": "In Progress"}
]
},
{
"project_id": "PRJ002",
"title": "Cloud Migration",
"manager": {
"name": "Mr. John Lee",
"email": "john@infra.net"
},
"phases": [
{"phase": "Assessment", "status": "Completed"}
]
}
]
We'll flatten all nested structures, including objects like manager, and summarize lists like phases into string fields. Each JSON record becomes a single flat row, with even complex nested data compactly represented in columns using readable summaries.
import json
# Helper: Flatten nested data and summarize list of dicts into strings
# e.g., [{"a":1},{"a":2}] → "a: 1; a: 2"
def flatten(data, parent_key='', sep='.'):
items = {}
for k, v in data.items():
new_key = f"{parent_key}{sep}{k}" if parent_key else k
if isinstance(v, dict):
items.update(flatten(v, new_key, sep=sep))
elif isinstance(v, list):
if all(isinstance(i, dict) for i in v):
items[new_key] = "; ".join(
", ".join(f"{ik}: {iv}" for ik, iv in i.items()) for i in v
)
else:
items[new_key] = ", ".join(map(str, v))
else:
items[new_key] = v
return items
# Load and flatten JSON
with open("projects_nested.json", "r", encoding="utf-8") as f:
raw_data = json.load(f)
flat_data = [flatten(record) for record in raw_data]
# Collect all unique keys from flattened data as headers
all_keys = set()
for item in flat_data:
all_keys.update(item.keys())
headers = list(sorted(all_keys)) # Consistent, sorted column order
This version of flatten() converts lists of dictionaries into concise summary strings (e.g., "phase: Design, status: Completed; phase: Development, status: In Progress"), making complex structures more compact for Excel output.
Step 2: Format and Export Excel with Spire.XLS
Now we’ll export the flattened project data to Excel, and use formatting features in Spire.XLS for Python to improve the layout and readability. This includes setting fonts, colors, merging cells, and automatically adjusting column widths for a professional report look.
from spire.xls import Workbook, Color, FileFormat
# Create workbook and worksheet
workbook = Workbook()
sheet = workbook.Worksheets[0]
sheet.Name = "Projects"
# Title row: merge and style
col_count = len(headers)
sheet.Range[1, 1, 1, col_count].Merge()
sheet.Range[1, 1].Text = "Project Report (Flattened JSON)"
title_style = sheet.Range["A1"].Style
title_style.Font.IsBold = True
title_style.Font.Size = 14
title_style.Font.Color = Color.get_White()
title_style.Color = Color.get_DarkBlue()
# Header row from flattened keys
for col, header in enumerate(headers):
cell = sheet.Range[2, col + 1]
cell.BorderAround() # Add outside borders to a cell or cell range
#cell.BorderInside() # Add inside borders to a cell range
cell.Text = header
style = cell.Style
style.Font.IsBold = True
style.Color = Color.get_LightGray()
# Data rows
for row_idx, row in enumerate(flat_data, start=3):
for col_idx, key in enumerate(headers):
sheet.Range[row_idx, col_idx + 1].Text = str(row.get(key, ""))
# Auto-fit columns and rows
for col in range(len(headers)):
sheet.AutoFitColumn(col + 1)
for row in range(len(flat_data)):
sheet.AutoFitRow(row + 1)
# Save Excel file
workbook.SaveToFile("output/projects_formatted.xlsx", FileFormat.Version2016)
workbook.Dispose()
This produces a clean, styled Excel sheet from a nested JSON file, making your output suitable for reports, stakeholders, or dashboards.
Code Explanation
- sheet.Range[].Merge(): merges a range of cells into one. Here we use it for the report title row (A1:F1).
- .Style.Font / .Style.Color: allow customizing font properties (bold, size, color) and background fill of a cell.
- .BorderAround() / .BorderInside(): add outside/inside borders to a cell range.
- AutoFitColumn(n): automatically adjusts the width of column
nto fit its content.
The Excel file generated after flattening the JSON data using Python:

Common Errors and Fixes in JSON ↔ Excel Conversion
Converting between JSON and Excel may sometimes raise formatting, encoding, or data structure issues. Here are some common problems and how to fix them:
| Error | Fix |
|---|---|
| JSONDecodeError or malformed input | Ensure valid syntax; avoid using eval(); use json.load() and flatten nested objects. |
| TypeError: Object of type ... is not JSON serializable | Use json.dump(data, f, default=str) to convert non-serializable values. |
| Excel file not loading or crashing | Ensure the file is not open in Excel; use the correct extension (.xlsx or .xls). |
| UnicodeEncodeError or corrupted characters | Set encoding="utf-8" and ensure_ascii=False in json.dump(). |
Conclusion
With Spire.XLS for Python, converting between JSON and Excel becomes a streamlined and reliable process. You can easily transform JSON data into well-formatted Excel files, complete with headers and styles, and just as smoothly convert Excel sheets back into structured JSON. The library helps you avoid common issues such as encoding errors, nested data complications, and Excel file format pitfalls.
Whether you're handling data exports, generating reports, or processing API responses, Spire.XLS provides a consistent and efficient way to work with .json and .xlsx formats in both directions.
Want to unlock all features without limitations? Request a free temporary license for full access to Spire.XLS for Python.
FAQ
Q1: How to convert JSON into Excel using Python?
You can use the json module in Python to load structured JSON data, and then use a library like Spire.XLS to export it to .xlsx. Spire.XLS allows writing headers, formatting Excel cells, and handling nested JSON via flattening. See the JSON to Excel section above for step-by-step examples.
Q2: How do I parse JSON data in Python?
Parsing JSON in Python is straightforward with the built-in json module. Use json.load() to parse JSON from a file, or json.loads() to parse a JSON string. After parsing, the result is usually a list of dictionaries, which can be iterated and exported to Excel or other formats.
Q3: Can I export Excel to JSON with Spire.XLS in Python?
Yes. Spire.XLS for Python lets you read Excel files and convert worksheet data into a list of dictionaries, which can be written to JSON using json.dump(). The process includes extracting headers, detecting used rows and columns, and optionally handling formatting. See Excel to JSON for detailed implementation.
How to Convert CSV to Excel (XLSX) in Python – Single & Batch Guide
2025-06-06 08:04:25 Written by zaki zouWhile working with CSV files is common in data processing, Excel (XLSX) often provides more advantages when it comes to data sharing, visualization, and large-scale analysis. In this guide, you’ll learn how to convert CSV to Excel in Python, including both single file and batch conversion methods. Whether you're automating reports or preparing data for further analysis, this guide will help you handle the conversion efficiently.

- Why Convert CSV to Excel
- Install Required Python Libraries
- Convert Single CSV to Excel
- Batch Convert CSV to XLSX
- FAQs
Why Convert CSV to Excel?
While CSV files are widely used for data storage and exchange due to their simplicity, they come with several limitations—especially when it comes to formatting, presentation, and usability. Converting CSV to Excel can bring several advantages:
Benefits of Converting CSV to Excel
- Better formatting support: Excel allows rich formatting options like fonts, colors, borders, and cell merging, making your data easier to read and present.
- Multiple worksheets: Unlike CSV files that support only a single sheet, Excel files can store multiple worksheets in one file, which is better for large datasets.
- Built-in formulas and charts: You can apply Excel formulas, pivot tables, and charts to analyze and visualize your data.
- Improved compatibility for business users: Excel is the preferred tool for many non-technical users, making it easier to share and collaborate on data.
Limitations of CSV Files
- No styling or formatting (plain text only)
- Single-sheet structure only
- Encoding issues (e.g., with non-English characters)
- Not ideal for large datasets or advanced reporting If your workflow involves reporting, data analysis, or sharing data with others, converting CSV to Excel is often a more practical and flexible choice.
Install Required Python Libraries
This guide demonstrates how to effortlessly convert CSV to Excel using Spire.XLS for Python. Spire.XLS is a powerful and professional Python Excel library that allows you to read, edit, and convert Excel files (both .xlsx and .xls) without relying on Microsoft Excel. Installing this CSV to Excel converter on your device is simple — just run the following command:
pip install Spire.XLS
Alternatively, you can download the Spire.XLS package manually for custom installation.
How to Convert CSV to Excel in Python: Single File
Now let’s get to the main part — how to convert a single CSV file to Excel using Python. With the help of Spire.XLS, this task becomes incredibly simple. All it takes is three easy steps: create a new workbook, load the CSV file, and save it as an Excel (.xlsx) file. Below is a detailed walkthrough along with a complete code example — let’s take a look!
Steps to convert a single CSV to Excel in Python:
- Create a Workbook instance.
- Load a sample CSV file using Workbook.LoadFromFile() method.
- Save the CSV file as Excel through Workbook.SaveToFile() method.
Below is the Python code to convert a CSV file to Excel. It also ignores parsing errors and automatically adjusts the column widths for better readability.
from spire.xls import *
from spire.xls.common import *
# Create a workbook
workbook = Workbook()
# Load a csv file
workbook.LoadFromFile("/sample csv.csv", ",", 1, 1)
# Set ignore error options
sheet = workbook.Worksheets[0]
sheet.Range["D2:E19"].IgnoreErrorOptions = IgnoreErrorType.NumberAsText
sheet.AllocatedRange.AutoFitColumns()
# Save the document and launch it
workbook.SaveToFile("/CSVToExcel1.xlsx", ExcelVersion.Version2013)

Warm Note: If you're only working with small files or doing some light testing, you can also use the free Spire.XLS. It's a great option for getting started quickly.
How to Batch Convert CSV to XLSX in Python
Another common scenario is when you need to convert multiple CSV files to Excel. Instead of manually replacing the file path and name for each one, there's a much more efficient approach. Simply place all the CSV files in the same folder, then use Python to loop through each file and convert them to Excel using the Workbook.SaveToFile() method. Let’s walk through the detailed steps below!
Steps to batch convert CSVs to Excel files in Python:
- Specify the file paths of input and output folders.
- Loop through all CSV files in the input folder.
- Create an object of Workbook class.
- Load each CSV file from the input folder with Workbook.LoadFromFile() method.
- Save the current CSV as an Excel file through Workbook.SaveToFile() method.
Here's the Python code to batch convert CSV to Excel (.XLSX):
import os
from spire.xls import *
input_folder = r"E:input\New folder"
output_folder = r"output\New folder"
# Loop through each CSV file
for csv_file in os.listdir(input_folder):
if csv_file.endswith(".csv"):
input_path = os.path.join(input_folder, csv_file)
output_name = os.path.splitext(csv_file)[0] + ".xlsx"
output_path = os.path.join(output_folder, output_name)
# Create a Workbook instance and load CSV files
workbook = Workbook()
workbook.LoadFromFile(input_path, ",", 1, 1)
# Save each CSV file as an Excel file
workbook.SaveToFile(output_path, ExcelVersion.Version2013)

The Conclusion
This guide showed you how to convert CSV to Excel in Python with step-by-step instructions and complete code examples. Whether you're working with a single CSV file or multiple files, Spire.XLS makes the process simple, fast, and hassle-free. Need help with more advanced scenarios or other Excel-related tasks? Feel free to contact us anytime!
FAQs about Converting CSV to Excel
Q1: How to convert CSV to Excel in Python without pandas?
A: You can use libraries like Spire.XLS, openpyxl, or xlsxwriter to convert CSV files without relying on pandas. These tools provide simple APIs to load .csv files and export them as xlsx—no Microsoft Excel installation required.
Q2: What is the easiest way to convert multiple CSV files to Excel in Python?
A: Just place all CSV files in one folder, then loop through them in Python and convert each using Workbook.SaveToFile(). This approach is ideal for batch processing. Alternatively, online converters can be a quick fix for occasional use.
Q3: How to auto-adjust column width when converting CSV to Excel in Python?
A: After loading the CSV, call worksheet.autoFitColumns() in Spire.XLS to automatically resize columns based on content before saving the Excel file.