PCEP Computer Programming and Python Fundamentals Practice Question
You are a developer in a data science team using Python for analysis. A colleague wrote a script that downloads a CSV file from a URL, parses it using csv.DictReader, and prints summary statistics. The script works on his machine but fails on yours with 'UnicodeDecodeError: 'utf-8' codec can't decode byte 0xe9 in position 100: invalid continuation byte'. The CSV file contains text in multiple languages, including French accents. The error occurs in the csv.DictReader call. You need to fix the script to work on any machine. Which approach is best?
⚠ Common exam trap
The PCEP exam often tests the misconception that 'utf-8-sig' or error-handling parameters like 'ignore' or 'replace' are universal fixes, when in fact they either address a different problem (BOM) or corrupt data, whereas specifying the correct encoding (latin-1) is the proper solution for files with extended Latin characters.
Answer choices
Why each option matters
Answer the question above first, then reveal the full breakdown to understand why each option is right or wrong.
Correct answer & explanation
✓
Open the file with encoding='latin-1' (or 'cp1252') to handle a wider range of bytes.
The error indicates the CSV file contains bytes that are not valid UTF-8, such as 0xe9 (é in Latin-1). Opening the file with encoding='latin-1' (or 'cp1252') maps every byte to the corresponding Unicode code point without decoding errors, which is a practical fix for CSV files with mixed-language text that are not strictly UTF-8. This approach ensures the file can be read on any machine regardless of the default system encoding.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✗
Read the file in binary mode and decode with 'utf-8' ignoring errors.
Why it's wrong here
Ignoring errors may discard important characters.
- ✗
Open the file with encoding='utf-8-sig' to handle BOM.
Why it's wrong here
utf-8-sig handles BOM, not invalid continuation bytes.
- ✗
Use the errors='replace' parameter in the open() call.
Why it's wrong here
Replaces undecodable characters with '?', leading to data loss.
- ✓
Open the file with encoding='latin-1' (or 'cp1252') to handle a wider range of bytes.
Why this is correct
latin-1 can decode any byte, preserving data, though may not be accurate for all characters.
Go deeper
Related to this question
About these practice questions
One of 498 original PCEP practice questions on Courseiva, each with a full explanation and wrong-answer analysis — not exam dumps or protected exam content. Learn why practice questions differ from exam dumps →
JA
Written by Johnson Ajibi, MSc IT Security
Senior Network & Security Engineer · founder of Courseiva
This PCEP practice question is part of Courseiva's free Python Institute certification practice question bank. Courseiva provides original exam-style practice questions with explanations, topic-based practice, mock exams, readiness tracking, and study analytics to help learners prepare for the PCEP exam.