如何在Python中编码(utf8mb4) [英] How to encode (utf8mb4) in Python

查看:804
本文介绍了如何在Python中编码(utf8mb4)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在Python中的ut8mb4中编码某些东西?我有两组数据:我正在从Parse迁移到我的新MySQL数据库,并且数据向前(仅与我的新数据库进行交谈)。我的数据库是utf8mb4,以便存储表情符号和重音字母。



当我在我的python脚本中时,第一组数据只能正确显示(表情符号和重音符号):


$ b $($ utf-8')

并且在PHP中从MySQL数据库读取时:

 code> $ row [message] = utf8_encode($ row [message]); 

第二组数据只能正确显示(当涉及到表情符号和重音符号时) T包含 utf8_encode($ row [message])部分。我正在尝试调和这些数据,以使两组数据正确地返回到我的iOS应用程序。请帮助!

解决方案

MySQL的 utf8mb4 encoding 只是标准的UTF-8 。



他们不得不添加该名称,以区别于 破坏仅支持BMP字符的UTF-8字符集



换句话说,您应该始终编码为UTF-8在与MySQL进行通话时,但考虑到数据库可能无法处理超出U + FFFF的Unicode码点,除非您使用 utf8mb4 MySQL端


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.

这篇关于如何在Python中编码(utf8mb4)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆