import csv
from pathlib import Path
FIELD_NAMES = ["Name", "Age", "Email Address"]
def create_sample_file(file_path: Path) -> None:
with file_path.open("w", newline="", encoding="utf-8") as file:
writer = csv.DictWriter(file, fieldnames=FIELD_NAMES)
writer.writeheader()
writer.writerow(
{
"Name": "Alex",
"Age": "19",
"Email Address": "alex@example.com",
}
)
def prompt_with_default(label: str, current_value: str) -> str:
response = input(f"{label} [{current_value}]: ").strip()
return response or current_value
def update_user_record(file_path: Path) -> dict[str, str]:
"""Prompt for replacements, rewrite the CSV, and return the saved row."""
raise NotImplementedErrorUpdate a pre-filled CSV record
Create a command-line editor for one record in user_data.csv. The file has the columns Name, Age, and Email Address. Show each current value as the default; pressing Enter keeps that value, while entering text replaces it. Write the resulting record back to the same file.
For example, if the stored name is Alex, its prompt should be:
Listing 1 shows the prompt with the current name.
The supplied create_sample_file function makes this exercise independent of any earlier file-writing task.
Use the supplied helpers and function template in Listing 2.
Test with a disposable path: call create_sample_file, update at least one field, then reopen the file and verify that it still contains one header and one data record.