【问题标题】:python remove IOS Emoji characters in a unicode str , to avoid DatabaseError: Incorrect string valuepython remove IOS Emoji characters in a unicode string , to avoid Database Error: Incorrect string value
【发布时间】:2014-03-28 07:18:54
【问题描述】:

我有一个 json {u'nickname':u'\U0001f638\U0001f638\u5bb6\u52c7'}。 将 nickname 保存到 db 时,它会引发:

DatabaseError: (1366, "Incorrect string value: '\\xF0\\x9F\\x98\\xB8\\xF0\\x9F..
.' for column 'nickname' at row 1")

我认为\U0001f638\U0001f638是问题所在,它们是某种图像代码。但是如何检测这样的字符串并删除它们呢?

【问题讨论】:

    标签: python mysql django


    【解决方案1】:

    我找到了答案here。 表情信息:http://punchdrunker.github.io/iOSEmoji/table_html/index.html

    \U0001f638 是 IOS 表情符号字符。 使用Martijn Pieters的代码:

    try:
        highpoints = re.compile(u'[\U00010000-\U0010ffff]')
    except re.error:
        # UCS-2 build
        highpoints = re.compile(u'[\uD800-\uDBFF][\uDC00-\uDFFF]')
    
    >>> import re
    >>> highpoints = re.compile(u'[\uD800-\uDBFF][\uDC00-\uDFFF]')
    >>> example = u'\U0001f638\U0001f638\u5bb6\u52c7'
    >>> highpoints.sub(u'', example)
    u'\u5bb6\u52c7'
    

    有效!

    【讨论】:

      猜你喜欢
      • 2022-12-01
      • 2019-09-10
      • 2016-08-10
      • 2022-12-27
      • 2020-11-18
      • 2022-12-19
      • 1970-01-01
      • 2022-10-25
      • 2022-08-18
      相关资源
      最近更新 更多