python编程怎么结束编码_如何在Python中编码(utf8mb4)

在将数据迁移至新的MySQL数据库时,遇到UTF-8MB4编码的问题,导致emoji和特殊字符显示不正确。解决方法是确保Python脚本中使用UTF-8编码与数据库交互,并注意PHP读取时的解码处理。对于不同数据集,可能需要调整编码解码策略以确保所有数据正确显示。
摘要由CSDN通过智能技术生成

1586010002-jmsa.png

How do I encode something in ut8mb4 in Python?

I have two sets of data: data I am migrating to my new MySQL database over from Parse, and data going forward (that talks only to my new database). My database is utf8mb4 in order to store emoji and accented letters.

The first set of data only shows up correctly (when emoji and accents are involved) when I have in my python script:

MySQLdb.escape_string(unicode(xstr(data.get('message'))).encode('utf-8'))

and when reading from the MySQL database in PHP:

$row["message"] = utf8_encode($row["message"]);

The second set of data only shows up correctly (when emoji and accents are involved) when I DON'T include the utf8_encode($row["message"]) portion. I am trying to reconcile these so that both sets of data are returned correctly to my iOS app. Please help!

解决方案

MySQL's utf8mb4 encoding is just standard UTF-8.

They had to add that name however to distinguish it from the broken UTF-8 character set which only supported BMP characters.

In other words, you should always encode to UTF-8 when talking to MySQL, but take into account that the database may not be able to handle Unicode codepoints beyond U+FFFF, unless you use utf8mb4 on the MySQL side.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值