You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python3从数据库读取文本遇编码相关错误,求解决方法

Fixing UnicodeEncodeError and String Encoding Mix-Ups in Python 3

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 the print() function can't accept directly (hence the TypeError: must be str, not bytes).
  • Calling .decode() on a string throws an error because decode() 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 04:26:16