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

用 antiword 将 doc 转换成 txt,怎么解决返回结果中文乱码问题?

  •  2
     
  •   CppOrJava · 2018-11-03 19:27:36 +08:00 · 2814 次点击
    这是一个创建于 1972 天前的主题,其中的信息可能已经有所发展或是发生改变。

    问题描述

    使用 python 调用 antiword 将 doc 转换成 txt,发现返回值是乱码,powershell 上用 antiword 也是乱码,在 git bash 上调用就没问题

    环境背景及自己尝试过哪些方法

    环境:windows 平台,py3.6 尝试过改 locale,还试过 antiword 的-m 参数,但是没什么用

    相关代码

    pipe = subprocess.Popen(
            ['antiword', filename],
            stdout=subprocess.PIPE, 
            stderr=subprocess.PIPE  )
    
    stdout, stderr = pipe.communicate()
    return stdout
    
    5 条回复    2018-11-04 10:09:05 +08:00
    newghost
        1
    newghost  
       2018-11-03 19:54:21 +08:00
    pdf2text
    lihongjie0209
        2
    lihongjie0209  
       2018-11-03 20:16:32 +08:00
    每一个进程执行的都有上下文的, 比如说环境变量, 你在 Python 中把环境变量打出来看看编码的配置项
    CppOrJava
        3
    CppOrJava  
    OP
       2018-11-03 20:19:59 +08:00
    @lihongjie0209 我试过改 locale,没有用
    代码:

    locale.setlocale(locale.LC_ALL, 'zh_CN.UTF-8')
    locale.setlocale(locale.LC_COLLATE,'zh_CN.UTF-8')
    locale.setlocale(locale.LC_CTYPE,'zh_CN.UTF-8')
    locale.setlocale(locale.LC_NUMERIC,'zh_CN.UTF-8')
    locale.setlocale(locale.LC_MONETARY,'zh_CN.UTF-8')
    locale.setlocale(locale.LC_TIME,'zh_CN.UTF-8')
    CppOrJava
        4
    CppOrJava  
    OP
       2018-11-03 20:25:19 +08:00
    @lihongjie0209 Linux 和 Windows 下 git bash 是正常的
    其实感觉跟 antiword 关系更大一些, cmd 下 chcp 65001 后 antiword 的输出仍然会有乱码
    但在 git bash 下调用 antiword, 就不会输出乱码
    zjsxwc
        5
    zjsxwc  
       2018-11-04 10:09:05 +08:00 via Android
    GB2312 的问题吧
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5922 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 34ms · UTC 02:32 · PVG 10:32 · LAX 19:32 · JFK 22:32
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.