V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
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
pc10201
V2EX  ›  Python

python2 如何将usc2与ansi互相进行编码转换?

  •  
  •   pc10201 · 2014-01-22 11:21:39 +08:00 · 5944 次点击
    这是一个创建于 3719 天前的主题,其中的信息可能已经有所发展或是发生改变。
    有一个字符串,
    usc2内容是:
    %u5728%u5e94%u7b54%u4e4b%u524d%u53d1%u9001%u8baf%u606f%u6210%u529f%uff0c%u8bf7%u7b49%u5f85%u7b54%u590d...

    对应的ansi的内容是:
    在应答之前发送讯息成功,请等待答复...

    在python2如何进行互相转换?
    4 条回复    2018-01-30 09:22:52 +08:00
    cute
        1
    cute  
       2014-01-22 12:34:48 +08:00   ❤️ 1
    s = %u5728%u5e94%u7b54%u4e4b%u524d%u53d1%u9001%u8baf%u606f%u6210%u529f%uff0c%u8bf7%u7b49%u5f85%u7b54%u590d...'
    print s.replace('%u','\\u').decode('raw_unicode_escape').encode('gbk')
    pc10201
        2
    pc10201  
    OP
       2014-01-22 15:00:40 +08:00
    @cute 谢谢~可以用~
    julyclyde
        3
    julyclyde  
       2014-01-23 11:18:00 +08:00
    ucs2?
    ycwang
        4
    ycwang  
       2018-01-30 09:22:52 +08:00
    一楼的答案我测试一下出来是乱码,encode 方式改为 utf-8 之后能够正常输出。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3064 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 11:01 · PVG 19:01 · LAX 04:01 · JFK 07:01
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.