在Flask / Python中处理表情符号的正确方法是什么?

时间:2014-09-09 12:20:22

标签: python amazon-web-services encoding flask emoji

我正在开发一个API,我对表情符号Flask / Python +表情符号编码感到疯狂:P

在本地服务器中我没有问题,表情符号图标“占据字符串总长度的两个位置”,而客户端(用HTML + Javascript编写)以相同的方式完成。但是当我将它部署到AWS EB时,表情符号图标“只占一个位置”,并且字符串的总长度更小,我完全不知道为什么会发生这种情况..

我写了一个小代码示例来说明发生了什么:

@api10.route('/prueba2', methods=['GET','POST'])
def prueba2():
    que = request.form.get("que", None)

    SEP = "\n"
    if request.form.get("web", None) == "ok":
        SEP = "<br />"

    out = "QUE: '%s'%s" % (que,SEP)
    out += "REP: '%s'%s%s" % (repr(que),SEP,SEP)
    out += "LENGTH: '%d'%s%s" % (len(que),SEP,SEP)
    out += "TYPE: '%s'%s%s" % (str(type(que)).replace("<", ""),SEP,SEP)
    for index,letter in enumerate(que):
        out += "%d -> %s%s" % (index,letter,SEP)

    return out, 200, {'Content-Type': 'text/html; charset=utf-8'}

本地回应: Local Response in the Chrome Postman

AWS EB响应: AWS EB Response in the Chrome Postman

响应标头在两者中都是相同的:

Content-Type →text/html; charset=utf-8
Date →Tue, 09 Sep 2014 11:47:03 GMT
Server →Werkzeug/0.9.6 Python/2.6.8

但在AWS EB中,“连接”“保持活跃”(当然“内容长度”不相等)

在Python 2.6上运行的两个实现(EC2使用该版本,在本地我有一个Virtualenv whit python26)

1 个答案:

答案 0 :(得分:2)

好的,我现在知道为什么会这样......

&GT;服务器端

虽然两个版本都运行在Python 2.6上,但AWS EB Python版本是使用UCS4支持编译的,而本地(Mac OS X)Python 2.6使用UCS2编译。 More info about UCS here

AWS EB EC2:
>>> import sys
>>> print sys.maxunicode
1114111
本地Python 2.6.8安装:
>>> import sys
>>> print sys.maxunicode
65535

最后我认为我们的项目使用Python 2.6 whit UCS4支持更好,所以我必须更新我的Python安装(Mac OS X 10.9.4):

下载并安装Python 2.6.8(与EC2实例相同):

$ curl -O https://www.python.org/ftp/python/2.6.8/Python-2.6.8.tgz
$ tar xzvf Python-2.6.8.tgz
$ cd Python-2.6.8
$ ./configure --disable-framework --disable-toolbox-glue OPT="-fast -arch x86_64 -Wall -Wstrict-prototypes -fno-common -fPIC" --enable-unicode=ucs4 LDFLAGS="-arch x86_64"
$ make
$ sudo make install

创建新的virtualenv并安装依赖项:

$ virtualenv -p /usr/local/bin/python2.6 venv_ayf_eb_26
$ . venv_ayf_eb_26/bin/activate
$ pip install -r requirements.txt

&GT;客户端

现在在客户端(Javascript)中,我们需要更新循环字符串的方式,因为ECMAScript 5-使用UCS2

所以要阅读&#34;真实的字符串/符号长度&#34;我们使用:

String.prototype.getSymbols = function() {
    var length = this.length;
    var index = -1;
    var output = [];
    var character;
    var charCode;
    while (++index < length) {
        character = this.charAt(index);
        charCode = character.charCodeAt(0);
        if (charCode >= 0xD800 && charCode <= 0xDBFF) {
            // note: this doesn’t account for lone high surrogates
            output.push(character + this.charAt(++index));
        } else {
            output.push(character);
        }
    }
    return output;
};
String.prototype.realLength = function() {
    return this.getSymbols().length;
};

循环:

// GET original_text over REST API
text = original_text.getSymbols();
for ( var i=0; i<original_text.length; i++) { /* DO SOMETHING */ }

参考

  1. The Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Character Sets (No Excuses!) - Joel Spolsky
  2. Unipain: Pragmatic Unicode - Ned Batchelder
  3. Universal Character Set - 维基百科
  4. ECMAScript - 维基百科
  5. ECMAScript® Language Specification (5.1) - Ecma International
  6. JavaScript has a Unicode problem - Mathias Bynens
  7. Python, convert 4-byte char to avoid MySQL error “Incorrect string value:” - StackOverflow
  8. How to find out if Python is compiled with UCS-2 or UCS-4? - StackOverflow