终端无法正确显示UTF-8编码问题——以LPTHW练习23为例
Hey there! As a fellow LPTHW learner, I’ve been through this exact hiccup—let’s break down what’s happening and get those characters displaying correctly.
Common Reasons for the Question Marks
Terminal Encoding Mismatch
The biggest culprit is usually that your terminal’s default encoding doesn’t match what you’re using in your code. If you’re encoding/decode with UTF-8 but your terminal is set to something else (like GBK on Windows or ISO-8859-1 on older systems), it can’t render characters outside its supported set, so it replaces them with question marks.Error Handling Parameter
If you ran your script withreplaceas theerrorargument (e.g.,python ex23.py utf-8 replace), Python will intentionally replace unencodeable/decodeable characters with question marks instead of throwing an error. This is expected behavior for thereplacehandler, but easy to overlook when following exercise steps.Terminal Font Limitations
Even with correct encoding, your terminal’s font might not support rare or non-Latin characters. For example, some fonts don’t include scripts like Tibetan or Cherokee, so those characters show up as question marks or empty squares.
Fixes to Try
Let’s work through these step by step:
1. Run the Script with the Right Encoding and Error Handler
Your code opens languages.txt with UTF-8 (which is correct for modern text files). When launching the script, pass utf-8 as the encoding and strict as the error handler to catch issues early:
python ex23.py utf-8 strict
If you get a UnicodeEncodeError, it’ll tell you exactly which character is causing the problem—super helpful for troubleshooting.
2. Set Your Terminal to Use UTF-8
Linux/macOS:
Check your current terminal encoding with:echo $LANGIt should output something like
en_US.UTF-8. If not, set it temporarily:export LANG=en_US.UTF-8For permanent changes: On macOS, go to Terminal > Settings > Profiles > Advanced and set "Character encoding" to UTF-8. On Linux, edit
/etc/locale.confto setLANG=en_US.UTF-8and runlocale-gen(varies slightly by distro).Windows Command Prompt/PowerShell:
Check your current code page:chcpSwitch to UTF-8 by running:
chcp 65001Then change your terminal font to a Unicode-friendly option (like Consolas, Microsoft YaHei, or Segoe UI Symbol) via the terminal’s properties menu.
3. Confirm languages.txt Is Saved as UTF-8
Sometimes the file itself might be in a different encoding (like GB2312). Open it in VS Code—look at the bottom-right corner; it should show "UTF-8". If not, click that label, select "Reopen with encoding", pick UTF-8, then save the file again.
4. Switch to a Unicode-Friendly Font
If encoding is fixed but you still see question marks, your font is missing those characters. Try these options:
- macOS: SF Pro or Menlo
- Windows: Segoe UI Symbol or Consolas
- Linux: Noto Sans (built to support nearly all Unicode characters)
Quick Recap
- Run the script with
utf-8 strictto catch encoding errors upfront. - Set your terminal to use UTF-8 encoding.
- Verify
languages.txtis saved as UTF-8. - Use a font that supports wide Unicode character sets.
Encoding stuff feels tricky at first, but it gets way easier once you wrap your head around how terminals and text encoding interact. You’ve got this!
内容的提问来源于stack exchange,提问作者rumblingThunder

