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

请教 Python 操作 Sqlite3 数据库和 Webpy 关于日期编码的问题

  •  
  •   Sargas · 2015-11-12 19:35:13 +08:00 · 2635 次点击
    这是一个创建于 3085 天前的主题,其中的信息可能已经有所发展或是发生改变。
    本人自学 Python 不久。今天学习用 Python 操作 Sqlite3 数据库时遇到一个问题,向大家请教一下。
    Windows 环境下, UTF8 编码的数据库,其中的日期字段,在数据库内是 datetext 类型。直接写 Python 查询,返回显示“ 2015 年 11 月 12 日 星期四”;同样的代码,输出到 webpy 之后,在网页中显示的是 u'2015\u5e7412\u670831\u65e5 \u661f\u671f\u56db'这种。在数据库连接中设置 con.text_factory = str 之后, web 输出变成'2015\xe5\xb9\xb412\xe6\x9c\x8824\xe6\x97\xa5 \xe6\x98\x9f\xe6\x9c\x9f\xe5\x9b\x9b',这种。
    我应该怎么处理才能让代码在 web 页面中也是显示成“ 2015 年 11 月 12 日 星期四”?
    3 条回复    2015-11-13 00:34:15 +08:00
    goodryb
        1
    goodryb  
       2015-11-12 22:54:21 +08:00
    >>> a=u'2015\u5e7412\u670831\u65e5 \u661f\u671f\u56db'
    >>> print a.encode('gbk')
    2015 年 12 月 31 日 星期四
    Sargas
        2
    Sargas  
    OP
       2015-11-12 23:08:12 +08:00
    @goodryb 谢谢!这个办法我知道,先按这个办法来吧。
    liujianpc
        3
    liujianpc  
       2015-11-13 00:34:15 +08:00 via iPhone
    Python 的编码问题和版本问题,真的很蛋疼
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1258 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 18:05 · PVG 02:05 · LAX 11:05 · JFK 14:05
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.