推荐学习书目
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
oldbird
V2EX  ›  Python

py2 怎么将汉字转换为 XML 里表示的&#xxxx 形式?

  •  
  •   oldbird · Jan 24, 2021 · 2623 views
    This topic created in 1934 days ago, the information mentioned may be changed or developed.

    &#xxxx 是什么编码格式? py2 怎么将汉字转换为这种形式? XML 里用的

    6 replies    2021-01-25 13:48:19 +08:00
    ClericPy
        1
    ClericPy  
       Jan 24, 2021
    # -*- coding: utf-8 -*-

    from html import escape

    print(escape(u'中文').encode('ascii', 'xmlcharrefreplace').decode('u8'))

    python - Convert HTML entities to Unicode and vice versa - Stack Overflow - https://stackoverflow.com/questions/701704/convert-html-entities-to-unicode-and-vice-versa
    mxT52CRuqR6o5
        2
    mxT52CRuqR6o5  
       Jan 24, 2021
    HTML 转义字符?
    bertonzh
        3
    bertonzh  
       Jan 24, 2021   ❤️ 1
    xxxx 是十进制的 unicode,所以你只需要把每个字符转成 code 就行。
    或者 &#xyyyy; 类似 yyyy 是十六进制格式。
    oldbird
        4
    oldbird  
    OP
       Jan 24, 2021
    @ClericPy
    打印结果为 ÖÐÎÄ
    怎么让一个汉字对应&#xxxx; ??
    ClericPy
        5
    ClericPy  
       Jan 24, 2021
    @oldbird 没懂你说的具体是什么... 举个例子? 我打印的直接丢 HTML 里就是中文俩字

    [Running] python2 -u "demo.py"
    中文
    no1xsyzy
        6
    no1xsyzy  
       Jan 25, 2021
    @oldbird 文件不要保存为 GBK
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   3777 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 45ms · UTC 04:56 · PVG 12:56 · LAX 21:56 · JFK 00:56
    ♥ Do have faith in what you're doing.