让 python3 下面的写的程序,能运行在 python2.7 上 - 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
yakczh

让 python3 下面的写的程序,能运行在 python2.7 上

  •  
  •   yakczh Oct 27, 2014 7459 views
    This topic created in 4215 days ago, the information mentioned may be changed or developed.
    filename='gbk.txt'
    try:
    s=open(filename,encoding='gbk').read()
    print "OK"
    except:
    print "Error"


    python3指定编码读文件的时候,如果编码不是指定的编码,会报错,上面这段代码在python3上运行是正确的,但是放到 python2.7下运行结果就不对 python2.7读gbk文件会提示Error, 2.7版本下有什么设置可以让这段代码在两个版本下运行结果一致吗?
    9 replies    2014-10-30 23:37:25 +08:00
    xunyu
        1
    xunyu  
       Oct 27, 2014
    2to3和3to2
    Daniel65536
        2
    Daniel65536  
       Oct 27, 2014
    from io import open

    在python2下:
    open(name[, mode[, buffering]])
    io.open(file[, mode[, buffering[, encoding[, errors[, newline[, closefd=True]]]]]])
    em70
        3
    em70  
       Oct 27, 2014
    3和2不兼容,2有2的写法,不能直接放过来
    lincanbin
        4
    lincanbin  
       Oct 27, 2014
    要两个版本下运行结果一致,就写两套代码,然后拿sys.version_info出来判断一下决定用那套
    ericls
        5
    ericls  
       Oct 27, 2014 via Android
    from io import open
    xucheng
        6
    xucheng  
       Oct 27, 2014
    import six
    fghzpqm
        7
    fghzpqm  
       Oct 29, 2014
    Python 2:

    $ ipython
    Python 2.7.8 (default, Aug 20 2014, 12:09:53)

    In [1]: import codecs

    In [2]: print codecs.open('GBK', encoding='gbk').read()
    你好

    Python 3:

    $ ipython
    Python 3.4.2 (default, Oct 29 2014, 17:32:14)

    In [1]: import codecs

    In [2]: print(codecs.open('GBK', encoding='gbk').read())
    你好
    yakczh
        8
    yakczh  
    OP
       Oct 29, 2014
    @fghzpqm python3 输出正常

    python27下 报错
    ---------- python27 ----------
    Traceback (most recent call last):
    File "codec-read-gbk.py", line 5, in <module>
    print(codecs.open('gbk.txt', encoding='gbk').read())
    UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-7: ordinal not in range(128)

    输出完成 (耗时 0 秒) - 正常终止
    fghzpqm
        9
    fghzpqm  
       Oct 30, 2014
    @yakczh 看输出的异常信息:

    UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-7: ordinal not in range(128)

    是在 print 的时候将 unicode 用默认的 ASCII codec 编码(encode)的时候出错。和你直接:

    print u"你好"

    抛出的是同样的异常。

    也就是说,为了确保你 print 的 unicode 不抛异常,你需要显示的把 unicode 编码成你终端使用的文本编码。
    About     Help     Advertise     Blog     API     FAQ     Solana     5701 Online   Highest 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 50ms UTC 03:31 PVG 11:31 LAX 20:31 JFK 23:31
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86