Fundamentals 7 min read

Processing CSV, JSON, and XML Data in Python

This tutorial demonstrates how to read, write, and convert CSV, JSON, and XML data using Python's built‑in modules and popular libraries such as pandas and dicttoxml, providing clear code examples for each format and showing how to interconvert them efficiently.

Python Programming Learning Circle
Python Programming Learning Circle
Python Programming Learning Circle
Processing CSV, JSON, and XML Data in Python

Python's flexibility and extensive libraries make it a top choice for data handling, especially for formats like CSV, JSON, and XML that dominate everyday data exchange.

CSV Data – The article explains reading CSV files with the built‑in csv module, iterating rows, and writing new CSV files using csv.writer. Example code shows opening a file, creating a reader, extracting field names, iterating rows, and printing the first five rows, followed by code that writes a list of rows to a new CSV file.

import csv

filename = "my_data.csv"
fields = []
rows = []
# Reading csv file
with open(filename, 'r') as csvfile:
    # Creating a csv reader object
    csvreader = csv.reader(csvfile)
    # Extracting field names in the first row
    fields = csvreader.next()
    # Extracting each data row one by one
    for row in csvreader:
        rows.append(row)
# Printing out the first 5 rows
for row in rows[:5]:
    print(row)

# Writing CSV
import csv
fields = ['Name', 'Goals', 'Assists', 'Shots']
rows = [
    ['Emily', '12', '18', '112'],
    ['Katie', '8', '24', '96'],
    ['John', '16', '9', '101'],
    ['Mike', '3', '14', '82']
]
filename = "soccer.csv"
with open(filename, 'w+') as csvfile:
    csvwriter = csv.writer(csvfile)
    csvwriter.writerow(fields)
    csvwriter.writerows(rows)

JSON Data – Using Python's json module and pandas, the guide shows how to load JSON into a dictionary, convert it to a DataFrame, and write formatted JSON files with indentation and sorted keys. It also demonstrates converting a DataFrame back to JSON.

import json
import pandas as pd
# Read the data from file
with open('data.json') as f:
    data_listofdict = json.load(f)
# pandas read json
data_df = pd.read_json('data.json', orient='records')
# Write a dictionary to JSON with pretty formatting
with open('new_data.json', 'w+') as json_file:
    json.dump(data_listofdict, json_file, indent=4, sort_keys=True)
# Export DataFrame to JSON
export = data_df.to_json('new_data.json', orient='records')

XML Data – The article introduces Python's built‑in xml.etree.ElementTree module and the third‑party xmltodict library for parsing XML, converting it to a dictionary, and then optionally converting that dictionary to CSV, JSON, or a pandas DataFrame. Sample code parses an XML file, converts it to a dictionary, and writes the result as pretty‑printed JSON.

import xml.etree.ElementTree as ET
import xmltodict
import json

tree = ET.parse('output.xml')
xml_data = tree.getroot()
xmlstr = ET.tostring(xml_data, encoding='utf8', method='xml')

data_dict = dict(xmltodict.parse(xmlstr))
print(data_dict)
with open('new_data_2.json', 'w+') as json_file:
    json.dump(data_dict, json_file, indent=4, sort_keys=True)

Overall, the guide provides a concise, code‑first reference for developers who need to manipulate common data interchange formats in Python, illustrating both low‑level module usage and higher‑level library shortcuts.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JSONXMLpandasdata-processingdicttoxml
Python Programming Learning Circle
Written by

Python Programming Learning Circle

A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.