Python3从数据库读取文本遇编码相关错误,求解决方法
Let's break down what's happening here and how to fix it:
Root Cause
The UnicodeEncodeError pops up because when you print your string, Python is trying to use the ASCII codec (which only supports characters up to ordinal 128) to encode the Unicode character \xad (a soft hyphen). In Python 3, all strings are Unicode by default, but your terminal or stdout might be set to an ASCII encoding, causing the failure.
Your subsequent attempts ran into issues because:
- Calling
.encode()on a string returns bytes, which theprint()function can't accept directly (hence theTypeError: must be str, not bytes). - Calling
.decode()on a string throws an error becausedecode()is meant for converting bytes to strings—strings don't have this method.
Solutions
Here are a few straightforward fixes to get your code working:
1. Force stdout to use UTF-8
This ensures your output stream can handle all Unicode characters. Add this at the start of your script:
import sys sys.stdout.reconfigure(encoding='utf-8')
Now your original print statement will work as-is:
print("%s | %s" % (time.strftime("%Y-%m-%d %H:%M:%S"), message))
2. Handle encoding errors explicitly when printing
If you can't change the stdout encoding, you can encode the string with an error handler (like replacing unencodable characters) and convert it back to a string for printing:
# Replace unencodable characters with � (use 'ignore' to drop them instead) safe_message = message.encode('utf-8', errors='replace').decode('utf-8') print("%s | %s" % (time.strftime("%Y-%m-%d %H:%M:%S"), safe_message))
3. Use modern string formatting (optional but cleaner)
Python 3.6+ supports f-strings, which are more readable than the old % formatting:
print(f"{time.strftime('%Y-%m-%d %H:%M:%S')} | {safe_message}")
Quick Recap
- Avoid mixing
encode()/decode()on strings unless you explicitly need to convert to/from bytes. - Always ensure your output stream uses a Unicode encoding (like UTF-8) to handle special characters.
内容的提问来源于stack exchange,提问作者djuarezg

