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

求助|python 调用 shell 后, shell 输出的编码问题

  •  
  •   soratadori · 2015-07-30 05:32:57 +08:00 · 2755 次点击
    这是一个创建于 3199 天前的主题,其中的信息可能已经有所发展或是发生改变。
    python3.4

    使用os.popen或者subprocess.getstatusoutput来调用shell的命令,然后接受shell的输出再显示出来。例如:
    print( os.popen('type 1.txt').read() ) #1.txt为utf-8编码
    subprocess.getstatusoutput('type 1.txt') #这句和上面那句功能一样

    如果输出的内容是utf-8的话,python会出现“UnicodeDecodeError: 'gbk' codec can't decode……”的问题。然而这两个函数也没什么decode、encode给你用...怎么办?网上搜了一圈也没解决,大多数人遇到的是输入问题
    2 条回复    2015-07-30 20:54:12 +08:00
    iam36
        1
    iam36  
       2015-07-30 08:37:04 +08:00
    感觉应该是 Xnix环境的编码设置可以解决你的问题 。
    oscarzhao
        2
    oscarzhao  
       2015-07-30 20:54:12 +08:00 via Android
    shell的编码不是utf8吧,read()后面为什么不能加encode decode呢
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   752 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 24ms · UTC 21:32 · PVG 05:32 · LAX 14:32 · JFK 17:32
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.