Fundamentals 7 min read

Master Fast CSV, JSON, and XML Handling in Python – Code Samples Included

This guide demonstrates how to efficiently read, write, and convert CSV, JSON, and XML data using Python's built‑in modules, pandas, and third‑party libraries, providing clear code examples for each format and showcasing best practices for data processing.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
Master Fast CSV, JSON, and XML Handling in Python – Code Samples Included

Python's flexibility and ease of use make it a top choice for data processing and machine learning, especially with its powerful libraries.

CSV Data

CSV is the most common way to store tabular data, widely used in Kaggle competitions. Python's built‑in csv module can read and write CSV files, typically loading data into a list of lists.

import csv

filename = "my_data.csv"

fields = []
rows = []
# Reading csv file 
with open(filename, 'r') as csvfile:
    csvreader = csv.reader(csvfile)
    fields = csvreader.next()
    for row in csvreader:
        rows.append(row)
# Printing out the first 5 rows 
for row in rows[:5]:
    print(row)

Writing CSV files is equally straightforward: define field names and rows, create a writer object, and output the data.

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)

Using pandas, a CSV can be turned into a list of dictionaries, which can then be saved as JSON or converted to XML with dicttoxml.

import pandas as pd
from dicttoxml import dicttoxml
import json

# Building our dataframe
data = {'Name': ['Emily', 'Katie', 'John', 'Mike'],
        'Goals': [12, 8, 16, 3],
        'Assists': [18, 24, 9, 14],
        'Shots': [112, 96, 101, 82]}

df = pd.DataFrame(data, columns=data.keys())

# Convert to dictionary and save as JSON
data_dict = df.to_dict(orient="records")
with open('output.json', "w+") as f:
    json.dump(data_dict, f, indent=4)

# Convert to XML and save
xml_data = dicttoxml(data_dict).decode()
with open("output.xml", "w+") as f:
    f.write(xml_data)

JSON Data

JSON offers a concise, readable format that mirrors Python dictionaries. The built‑in json module makes reading and writing JSON trivial.

import json
import pandas as pd

# Read JSON file into a Python dictionary
with open('data.json') as f:
    data_listofdict = json.load(f)

# Load the same data with pandas
data_df = pd.read_json('data.json', orient='records')

# Write dictionary back 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 pandas DataFrame to JSON
export = data_df.to_json('new_data.json', orient='records')

XML Data

XML is more verbose than CSV or JSON, requiring more storage and bandwidth, but it provides features like namespaces and schema validation.

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)
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.

data-processingXMLCSVpandasfile-io
MaGe Linux Operations
Written by

MaGe Linux Operations

Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.

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.