推荐学习书目
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 · Jul 30, 2015 · 3342 views
    This topic created in 4005 days ago, the information mentioned may be changed or developed.
    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 replies    2015-07-30 20:54:12 +08:00
    iam36
        1
    iam36  
       Jul 30, 2015
    感觉应该是 Xnix环境的编码设置可以解决你的问题 。
    oscarzhao
        2
    oscarzhao  
       Jul 30, 2015 via Android
    shell的编码不是utf8吧,read()后面为什么不能加encode decode呢
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   5789 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 546ms · UTC 06:41 · PVG 14:41 · LAX 23:41 · JFK 02:41
    ♥ Do have faith in what you're doing.