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

Python3 写入中文(utf-8)到文本,还是会出现乱码?

  •  
  •   rogwan · 2017-09-04 16:53:42 +08:00 · 8744 次点击
    这是一个创建于 2397 天前的主题,其中的信息可能已经有所发展或是发生改变。

    MySQL 数据库里存的数据是 utf-8,python3 写入文本,还是会出乱码:

    news = News.query.filter_by(news_id=news_id).first()
    f = open('news.txt', 'w')
    f.write(news.content)
    f.close()
    

    news.content 文章内容是全英文的 OK,文章里面有中文就是乱码。

    16 条回复    2017-09-04 22:12:46 +08:00
    yunkchen
        1
    yunkchen  
       2017-09-04 16:57:09 +08:00   ❤️ 1
    rogwan
        2
    rogwan  
    OP
       2017-09-04 17:07:20 +08:00
    @yunkchen 试过加 encoding='utf-8':

    f = open('news.txt', 'w', encoding='utf-8')

    中文还是乱码。
    fanhaipeng0403
        3
    fanhaipeng0403  
       2017-09-04 17:09:39 +08:00   ❤️ 1
    数据库编码设置,驱动编码设置
    ltux
        4
    ltux  
       2017-09-04 17:13:07 +08:00   ❤️ 1
    先搞清楚 news.content 是 bytes 还是 str
    rogwan
        5
    rogwan  
    OP
       2017-09-04 17:27:57 +08:00
    @ltux print(type(news.content)) 是 str 啊

    news.content <class 'str'>
    geofvt
        6
    geofvt  
       2017-09-04 17:30:01 +08:00   ❤️ 1
    news.content 需要编码转换
    或者用 beautifulsoup 的 prettify()
    https://gist.github.com/ercJuL/97e27d199451d43f93f14d41625955c9
    petelin
        7
    petelin  
       2017-09-04 17:39:13 +08:00
    乱码贴出来看看
    rogwan
        8
    rogwan  
    OP
       2017-09-04 17:47:29 +08:00
    @petelin

    <p>中文字符</p>

    中文字符
    ltux
        9
    ltux  
       2017-09-04 18:06:15 +08:00
    我猜 new.content 里面是原内容以 utf-8 编码之后又原封不动转换成了对应的 str。所以解决方法是先原封不动转换成 bytes, 再按 utf-8 解码,即:news.content.encode('latin-1').decode('utf-8')

    说到底还是数据库驱动设置的问题。
    ltux
        10
    ltux  
       2017-09-04 18:09:44 +08:00
    如上,写入的时候把 f.write( news.content ) 换成 f.write( news.content.encode('latin-1').decode('utf-8') ) 八成就行了。
    xhp281
        11
    xhp281  
       2017-09-04 18:34:38 +08:00   ❤️ 1
    f = open('news.txt', 'w', encoding='utf-8') 这样子不行?你打印的时候是不是乱码
    rogwan
        12
    rogwan  
    OP
       2017-09-04 19:33:35 +08:00
    @ltux
    @xhp281

    呃,貌似不是数据库里编码的问题,有点奇怪,IDE 环境里 f.write('中文字符') 写中文都是乱码。直接 IDLE 写 f.write('中文字符') 没问题。把 IDE 里的 *.py 文件全部重新 encode 一下,现在好了
    mcfog
        13
    mcfog  
       2017-09-04 19:53:23 +08:00 via Android
    赌五毛楼主是 windows 环境,赌一块楼主不知道什么是 BOM
    rogwan
        14
    rogwan  
    OP
       2017-09-04 20:05:21 +08:00 via Android
    @mcfog 你先把 5 毛掏出来先,哈哈😄 全部是 ubuntu 环境 server 和 desktop (只是用了一次 idle ) bom 在这个 debug 里为什么要出现?
    flynaj
        15
    flynaj  
       2017-09-04 20:28:56 +08:00 via Android
    .py 文件也要保存成 UTF-8,新手问题
    likuku
        16
    likuku  
       2017-09-04 22:12:46 +08:00
    先确认你数据库内数据表的编码是 utf8,数据库连接是 utf8,数据存取时是 utf8,原始数据是 utf8 编码
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   4676 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 33ms · UTC 10:03 · PVG 18:03 · LAX 03:03 · JFK 06:03
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.