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

Python3读取MySQL数据后Unicode符号显示异常求助

Hey there, let's figure out why those Unicode symbols are showing up as escaped \u sequences instead of the actual characters/emojis.

First, let's recap what's happening with your current workflow: When you store Unicode data in a MySQL column using latin1, MySQL doesn't convert those characters—it just saves the raw UTF-8 bytes as individual latin1 characters (since latin1 maps every byte to a unique character). Your str.encode('latin-1').decode('utf-8') step is technically the right way to reverse that, but if you're seeing escaped sequences, here are the most likely fixes:

Possible Solutions

1. Unescape Unicode sequences stored in the database

It’s possible the data was saved with escaped Unicode strings (like \u1F600 instead of the 😀 emoji) rather than raw UTF-8 bytes. To convert these escaped sequences back to actual characters, use Python’s ast.literal_eval (safer than manual decoding):

import ast
# After your initial encode/decode step
processed_str = str.encode('latin1').decode('utf-8')
fixed_str = ast.literal_eval(f'"{processed_str}"')

This will properly parse any escaped \u sequences into their corresponding Unicode characters.

2. Check if your environment is just displaying the string incorrectly

Sometimes, older Python shells or terminals that don’t fully support Unicode will show escaped \u sequences even when the underlying string is correct. To verify, write the string to a UTF-8 encoded file:

with open('unicode_test.txt', 'w', encoding='utf-8') as f:
    f.write(processed_str)

Open that file in a modern text editor (like VS Code or Notepad++)—if the emojis/Unicode characters display correctly, the issue is just with your terminal/shell output, not the string itself.

3. Fix the database charset long-term

Storing Unicode data in a latin1 column is a temporary workaround. The proper solution is to switch your database/table to use utf8mb4, which supports all Unicode characters (including emojis). Here’s how to do it:

  • Alter your database:
    ALTER DATABASE your_database_name CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
    
  • Alter your table and column:
    ALTER TABLE your_table_name CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
    
  • When connecting to MySQL, use charset='utf8mb4' instead of latin1—then you won’t need the encode/decode hack at all.

内容的提问来源于stack exchange,提问作者com

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.28 07:15:38