Home Uncategorized Python File Handling, JSON/CSV Processing, Regular Expressions, Error...
Uncategorized

Python File Handling, JSON/CSV Processing, Regular Expressions, Error Handling: 20 Coding Interview Questions

File handling is essential for every Python program. Reading/writing JSON, CSV, text enables data processing at scale. Regular expressions pattern-match text. Exception handling manages errors gracefully. Every system processes files, and interviews test these fundamentals.

File I/O Basics

Opening & Reading: open() returns file object. modes: ‘r’ (read), ‘w’ (write), ‘a’ (append), ‘b’ (binary). read() entire file, readline() single line, readlines() list. Context manager (with statement) auto-closes: exception-safe. Always use with statement to prevent resource leaks. Binary mode: images, compiled files. Text mode: JSON, CSV, logs.

with open(“data.txt”, “r”) as f:
content = f.read()
with open(“data.txt”) as f:
for line in f:
print(line.strip())
lines = open(“data.txt”).readlines()

Writing & Appending: open(filename, ‘w’) overwrites, ‘a’ appends. write() single string, writelines() list (no newlines added). print() with file parameter. Buffering: automatic flush on close or manual flush(). Encoding: default utf-8, specify if needed (latin1, ascii).

with open(“output.txt”, “w”) as f:
f.write(“Hello\n”)
f.writelines([“Line 1\n”, “Line 2\n”])
with open(“log.txt”, “a”) as f:
print(“Info”, file=f)
with open(“data.txt”, encoding=”utf-8″) as f:
content = f.read()

JSON: Parsing & Serialization: json.load(file), json.loads(string) parse JSON. json.dump(object, file), json.dumps(object) serialize. Handles dict, list, str, int, float, bool, None. indent parameter for pretty-printing. Custom encoder for non-standard types. Error handling: JSONDecodeError on malformed JSON.

import json
with open(“data.json”) as f:
data = json.load(f)
obj = {“name”: “Alice”, “age”: 30}
with open(“out.json”, “w”) as f:
json.dump(obj, f, indent=2)
data = json.loads(‘{“key”: “value”}’)

CSV: Tabular Data: csv.reader() parses CSV, returns rows as lists. csv.DictReader() as dicts (headers as keys). csv.writer() writes rows. csv.DictWriter() writes dicts. dialect=’excel’ (default comma), ‘excel-tab’ for TSV. Skip header with next(). Large files: stream (no memory load).

import csv
with open(“data.csv”) as f:
reader = csv.DictReader(f)
for row in reader:
print(row)
with open(“output.csv”, “w”) as f:
writer = csv.DictWriter(f, fieldnames=[“name”, “age”])
writer.writeheader()
writer.writerow({“name”: “Alice”, “age”: 30})

Regular Expressions: Pattern Matching: re.search() finds first match, re.findall() all matches, re.sub() replace. Patterns: \d (digit), \w (word), . (any), * (0+), + (1+), ? (0-1), [] (char class), ^ (start), $ (end). Flags: re.IGNORECASE, re.MULTILINE. Groups: parentheses capture substrings.

import re
text = “Email: user@example.com”
match = re.search(r”\w+@\w+”, text)
print(match.group())
emails = re.findall(r”\w+@[\w.]+”, text)
text = re.sub(r”\d+”, “X”, “Id: 123″)
pattern = r”(\w+)@([\w.]+)”
match = re.search(pattern, text)
print(match.groups())

Exception Handling: Error Management: try-except-finally structure. try executes, except catches errors, finally always runs (cleanup). Multiple except blocks for different exceptions. Exception hierarchy: Exception → specific (FileNotFoundError, ValueError, JSONDecodeError). raise custom exceptions. else block executes if no exception.

try:
with open(“file.json”) as f:
data = json.load(f)
except FileNotFoundError:
print(“File not found”)
except json.JSONDecodeError as e:
print(f”Invalid JSON: {e}”)
else:
print(“Success”)
finally:
print(“Cleanup”)

5 Complete Solved Coding Problems

Question 1: JSON Parsing with Error Handling

Read JSON file (users list). Parse safely, handle missing/malformed. Count users by age group (18-25, 26-35, 35+). Output results as JSON.

import json
try:
with open(“users.json”) as f:
users = json.load(f)
age_groups = {“18-25”: 0, “26-35”: 0, “35+”: 0}
for user in users:
age = user.get(“age”)
if age is None:
continue
if age <= 25: age_groups["18-25"] += 1 elif age <= 35: age_groups["26-35"] += 1 else: age_groups["35+"] += 1 with open("result.json", "w") as f: json.dump(age_groups, f, indent=2) except (FileNotFoundError, json.JSONDecodeError) as e: print(f"Error: {e}")

Explanation: try-except handles file/JSON errors. user.get() safe access (None if missing). Conditional age grouping. json.dump() serializes output. Finally: resource cleanup automatic via with statement.

Google – Software Engineer

Question 2: CSV Processing & Filtering

Read sales.csv (date, product, amount). Filter last 30 days. Group by product, sum amounts. Write filtered CSV.

import csv
from datetime import datetime, timedelta
today = datetime.now()
thirty_days_ago = today – timedelta(days=30)
sales_by_product = {}
try:
with open(“sales.csv”) as f:
reader = csv.DictReader(f)
for row in reader:
date = datetime.strptime(row[“date”], “%Y-%m-%d”)
if date >= thirty_days_ago:
product = row[“product”]
amount = float(row[“amount”])
sales_by_product = sales_by_product.get(product, 0) + amount
with open(“filtered_sales.csv”, “w”) as f:
writer = csv.DictWriter(f, fieldnames=[“product”, “total_amount”])
writer.writeheader()
for product, amount in sales_by_product.items():
writer.writerow({“product”: product, “total_amount”: amount})
except (FileNotFoundError, ValueError) as e:
print(f”Error: {e}”)

Explanation: csv.DictReader reads with headers as keys. datetime parsing with strptime. Filtering by date comparison. Aggregation via dict. csv.DictWriter outputs formatted. Exception handling for file/parsing errors.

Meta – Backend Engineer

Question 3: Regex Email Extraction

Read log file (text). Extract all emails using regex. Deduplicate, save to file. Count by domain.

import re
try:
with open(“log.txt”) as f:
content = f.read()
emails = set(re.findall(r”\w+@[\w.]+\.\w+”, content))
domain_count = {}
for email in emails:
domain = email.split(“@”)[1]
domain_count[domain] = domain_count.get(domain, 0) + 1
with open(“emails.txt”, “w”) as f:
for email in sorted(emails):
f.write(email + “\n”)
print(f”Found {len(emails)} unique emails”)
print(domain_count)
except FileNotFoundError:
print(“Log file not found”)

Explanation: re.findall() extracts all matches. Set deduplicates. Domain extraction via split(). Aggregation into dict. File writing via write(). Sorted output for consistency.

Amazon – Software Engineer

Question 4: JSON Transformation

Read old_format.json (nested). Transform to flat CSV: extract id, name, city. Handle missing fields gracefully.

import json
import csv
try:
with open(“old_format.json”) as f:
data = json.load(f)
rows = []
for record in data:
address = record.get(“address”, {})
rows.append({“id”: record.get(“id”), “name”: record.get(“name”), “city”: address.get(“city”)})
with open(“flat.csv”, “w”) as f:
writer = csv.DictWriter(f, fieldnames=[“id”, “name”, “city”])
writer.writeheader()
writer.writerows(rows)
except (FileNotFoundError, json.JSONDecodeError, KeyError) as e:
print(f”Error: {e}”)

Explanation: json.load() parses nested structure. .get() with defaults handles missing fields. Flattening nested dicts. csv.writerows() efficient bulk write. Error hierarchy catches specific issues.

Apple – Backend Engineer

Question 5: Log Parsing with Regex & Timestamps

Parse app.log (format: [YYYY-MM-DD HH:MM:SS] LEVEL message). Extract errors, count per hour. Output summary JSON.

import re
import json
try:
pattern = r”\[(\d{4}-\d{2}-\d{2} \d{2}):.*?\] (\w+) (.*)”
errors_by_hour = {}
with open(“app.log”) as f:
for line in f:
match = re.search(pattern, line)
if match:
timestamp, level, msg = match.groups()
if level == “ERROR”:
hour = timestamp.split(” “)[0] + ” ” + timestamp.split(“:”)[0].split(” “)[1]
errors_by_hour[hour] = errors_by_hour.get(hour, 0) + 1
with open(“error_summary.json”, “w”) as f:
json.dump(errors_by_hour, f, indent=2)
except (FileNotFoundError, AttributeError) as e:
print(f”Error: {e}”)

Explanation: Regex groups capture timestamp, level, message. Conditional processing (errors only). Hour extraction via string split. Aggregation by hour. JSON output for readability.

Microsoft – Senior Engineer

15 Practice Questions (Test Your Understanding)

Question 6: Read large CSV (10M rows). Stream processing (no full load). Count rows matching filter. Memory-efficient approach.

Question 7: Regex: extract phone numbers (format: (XXX) XXX-XXXX). Validate format before saving.

Question 8: Read JSON array, write CSV. Handle special characters (quotes, commas). Proper escaping.

Question 9: Log file with timestamps. Find events in time window (2024-01-01 to 2024-01-31). Extract matching lines.

Question 10: Regex: find URLs (http/https). Extract domain, path, query params if present.

Question 11: Read config.ini file. Parse sections, keys, values. Handle missing sections gracefully.

Question 12: JSON with datetime strings. Parse, convert to datetime objects, filter by date range.

Question 13: CSV with unicode characters. Read with proper encoding. Write to different encoding.

Question 14: Regex: validate email address. Accept standard formats. Reject invalid patterns.

Question 15: Read multiple JSON files from directory. Aggregate data, write combined output.

Question 16: Exception hierarchy: catch specific errors (FileNotFoundError, JSONDecodeError, ValueError) separately.

Question 17: Regex: extract hashtags from text. Deduplicate, count frequency.

Question 18: Write CSV preserving order of columns using OrderedDict or dict (Python 3.7+).

Question 19: Regex: split text by sentences, paragraphs. Preserve structure.

Question 20: JSON with nested structure. Flatten using recursion. Output as single-level JSON.

Master File Handling

Get our comprehensive Data Analytics: Ace All The Interview Concepts course.

Scroll to Top