推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
lxy
V2EX  ›  Python

怎么把 Python 的字符串转换成 JavaScript 的 unicode?

  •  
  •   lxy · Jul 5, 2016 · 3412 views
    This topic created in 3629 days ago, the information mentioned may be changed or developed.
    Python3 。
    比如把 “技术” 转换成 '%u6280%u672F' 。
    而在 py 中编码出来的是 '\xe6\x8a\x80\xe6\x9c\xaf'、'%E6%8A%80%E6%9C%AF',毫不相关。
    9 replies    2016-07-05 21:56:42 +08:00
    BOYPT
        1
    BOYPT  
       Jul 5, 2016   ❤️ 2
    print("中文".encode('raw-unicode-escape'))
    lonelinsky
        2
    lonelinsky  
       Jul 5, 2016   ❤️ 1
    想不到现成的方法,不过大概可以这样:
    ''.join(['%u{:x}'.format(ord(c)) for c in s])
    lowzoom
        3
    lowzoom  
       Jul 5, 2016   ❤️ 1
    ''.join(hex(ord(s)) for s in '技术').replace('0x','%u')
    lowzoom
        4
    lowzoom  
       Jul 5, 2016
    '技术'.encode('raw-unicode-escape').replace('\\','%')
    ChiChou
        5
    ChiChou  
       Jul 5, 2016   ❤️ 1
    你是在哪看到的 %u 表示……
    seki
        6
    seki  
       Jul 5, 2016
    urlib 的 quote_plus
    seki
        7
    seki  
       Jul 5, 2016
    3 的话就是 urllib.parse.quote_plus()
    shyling
        8
    shyling  
       Jul 5, 2016 via iPad
    js 的不是\u{}么。。。。
    v2014
        9
    v2014  
       Jul 5, 2016
    >>> import json
    >>> json.dumps('技术')
    '"\\u6280\\u672f"'
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   3067 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 56ms · UTC 07:53 · PVG 15:53 · LAX 00:53 · JFK 03:53
    ♥ Do have faith in what you're doing.