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
kingseven
V2EX  ›  Python

求教:使用python的markdown库时中文编码问题(注:非乱码)

  •  
  •   kingseven · 2013-08-25 03:01:17 +08:00 · 5565 次点击
    这是一个创建于 3902 天前的主题,其中的信息可能已经有所发展或是发生改变。
    代码类似这样的:
    # coding = utf8

    text = '# 大标题 ## 第二号 ... '
    result = markdown.markdown(text)

    错误是:
    'ascii' codec can't encode characters in position 18-27: ordinal not in range(128)

    不论怎么encode/decode 都不行。用谷歌查找的结果基本上都是乱码,实在想不出来结果,只好在这拜托各位了
    先谢谢各位
    4 条回复    1970-01-01 08:00:00 +08:00
    013231
        1
    013231  
       2013-08-25 04:22:34 +08:00   ❤️ 1
    text = u'# 大标题 ## 第二号 ... '
    result = markdown.markdown(text)
    013231
        2
    013231  
       2013-08-25 04:26:23 +08:00
    或者:
    # encoding=utf8

    text = '# 大标题 ## 第二号 ... '
    result = markdown.markdown(text.decode('utf-8'))
    liushuaikobe
        3
    liushuaikobe  
       2013-08-25 09:56:11 +08:00   ❤️ 1
    正好之前有用到过这个库做过一个小项目,你可以去参考下:https://github.com/liushuaikobe/evermd

    # Markdown only accepts unicode input!

    text = '# 大标题 ## 第二号 ... '
    md = unicode(text, 'utf-8')
    markdown.markdown(md).encode('utf-8')
    kingseven
        4
    kingseven  
    OP
       2013-08-25 10:31:46 +08:00
    @013231
    @liushuaikobe
    谢谢。找到问题了。我的python默认编码是ascii,已经搞定。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2322 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 12:26 · PVG 20:26 · LAX 05:26 · JFK 08:26
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.