When working with real-world data, malformed files are a common pain point. Whether it's a CSV with inconsistent delimiters or a JSON file with missing quotes, these issues can bring your pipeline to a halt. Python offers powerful tools for data processing, but the key is knowing how to handle these edge cases without relying on external APIs or cloud services.
In this article, we'll walk through a practical technique for cleaning malformed JSON and CSV files using a self-contained Python script. This approach is ideal for developers who need to process data in isolated environments or want to avoid dependency on external services.
Let's start by examining a common issue: malformed JSON. A JSON file with missing commas or mismatched brackets can cause parsing errors. Here's how you can handle it with a simple script that attempts to fix minor syntax errors.
import json
import sys
def fix_json(file_path):
try:
with open(file_path, 'r') as f:
data = f.read()
# Attempt to parse with error tolerance
try:
parsed = json.loads(data)
print("✅ JSON parsed successfully.")
return parsed
except json.JSONDecodeError as e:
print(f"❌ JSON parse error: {e}")
# Attempt to fix by replacing missing commas
data = data.replace('},', '}, ')
data = data.replace('{', '{ ')
data = data.replace('}', ' }')
try:
parsed = json.loads(data)
print("✅ JSON fixed and parsed.")
return parsed
except json.JSONDecodeError as e:
print(f"❌ Could not fix JSON: {e}")
return None
except FileNotFoundError:
print(f"❌ File not found: {file_path}")
return None
if __name__ == '__main__':
if len(sys.argv) != 2:
print("Usage: python fix_json.py <file.json>")
sys.exit(1)
result = fix_json(sys.argv[1])
if result:
print("Processed data:", result)
This script reads a JSON file, attempts to parse it, and if it fails due to minor syntax errors, tries to fix them by adding spaces around braces and commas. It's a simple yet effective way to handle common JSON issues without external dependencies.
Now, let's look at CSV files, which can be even trickier due to inconsistent delimiters and missing fields. Here's a script that reads a CSV, handles missing values, and outputs a cleaned version.
import csv
import sys
def clean_csv(input_file, output_file):
with open(input_file, 'r') as infile, open(output_file, 'w', newline='') as outfile:
reader = csv.DictReader(infile)
fieldnames = reader.fieldnames
writer = csv.DictWriter(outfile, fieldnames=fieldnames)
writer.writeheader()
for row in reader:
# Handle missing values
for field in fieldnames:
if field not in row:
row[field] = ''
writer.writerow(row)
print(f"✅ Cleaned CSV saved to {output_file}")
if __name__ == '__main__':
if len(sys.argv) != 3:
print("Usage: python clean_csv.py <input.csv> <output.csv>")
sys.exit(1)
clean_csv(sys.argv[1], sys.argv[2])
This script reads a CSV file, checks for missing fields, and fills them with empty strings. It ensures that the output file has consistent structure and can be used in downstream processes.
Both of these tools are part of The Python Dev Toolkit Template Collection, a self-contained command-line tool that helps developers process data without external dependencies. Whether you're dealing with malformed JSON or inconsistent CSV files, this toolkit provides a reliable way to clean and process your data.
If you're working in an environment where you can't use APIs or cloud services, these scripts offer a lightweight, powerful solution. They're designed to be run from the command line, making them ideal for CI/CD pipelines or local development workflows.
For more advanced data processing tasks, or if you need to handle complex data structures, consider using the toolkit's full capabilities. It's a great choice for developers looking to streamline their workflow and enhance productivity.
You can find the toolkit at https://intellitools.gumroad.com/l/the-python-dev-toolkit-template-collection.
Top comments (0)