推荐学习书目
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
Gandum
V2EX  ›  Python

求助各位 V 友一个 Python 2 和 Python 3 代码转换的问题,请帮我迈过这个坎,谢谢!

  •  
  •   Gandum · Apr 1, 2018 · 3567 views
    This topic created in 2990 days ago, the information mentioned may be changed or developed.

    Python 2 代码:

    import os
    a = '谢谢大家帮助我。'
    b = a.decode('utf-8').encode('gbk')
    command = '7z x test.zip -p%s' % (b,)
    os.system(command)
    

    问题描述:

    • 现在有一个test.zip压缩包,密码为“谢谢大家帮助我。”这个压缩包是在 Windows 环境下用 winrar5.5 或者 winzip22 创建的。
    • 问题是由于此密码为 GBK 编码,所以在 Linux 命令:'7z x test.zip -p 谢谢大家帮助我。' 是解压不出来的。
    • 正确的解压方法应该是要把密码用 iconv 重新编码,其在 shell 里面直接的方法为: 'echo -n '谢谢大家帮助我' | iconv -f utf8 -t gbk | iconv -f iso-8859-1 -t utf8 | xargs -i 7z x t.zip -p{}'

    请问在 Python 3 里面应该怎么写才能让 os.system 正确执行?
    补充说明:

    • 我并不想使用我在描述里面那个超长的式子,希望像 Python 2 里面那样简洁,这应该是一个编码问题。
    • 我已经发现了下面这段 Python 3 代码可以 print 出和 Python 2 同样的内容(结果都是'7z x test.zip -pлл���Ұ����ҡ�'),但是 os.system 执行结果显示错误:
    import os
    a = '谢谢大家帮助我。'
    b = bytes(a,'gbk').decode('utf-8',errors='replace')
    command = '7z x t.zip -p%s' % (b,)
    os.system(command)
    
    9 replies    2018-04-02 08:56:07 +08:00
    kokutou
        1
    kokutou  
       Apr 1, 2018 via Android
    不如看看 python 库:zipfile ?
    gnaggnoyil
        2
    gnaggnoyil  
       Apr 1, 2018   ❤️ 1
    8102 年了怎么还有分不清 code point 和 byte 的人.`a.encode("gbk")`不就完了
    Gandum
        3
    Gandum  
    OP
       Apr 2, 2018
    @kokutou 这个我考虑过,不过 zipfile 库好像只能在 open 和 extract 时候输密码,我只是想 test 我的压缩包们(事实上我想执行的是 7z t )
    @gnaggnoyil 我之前不知道 os.system 可以输入 bytes,经你启发发现这一点,问题解决,Thank you !
    gnaggnoyil
        4
    gnaggnoyil  
       Apr 2, 2018   ❤️ 1
    @Gandum 讲道理你要是知道`errors='replace'`是在干什么的话就不会有这种问题了……
    Gandum
        5
    Gandum  
    OP
       Apr 2, 2018
    @gnaggnoyil 恩,我会去仔细看看的!不过这位朋友还请留步一下,请问在 zipfile 里面怎么解决这个问题呢?
    这里 ZipFile.extractall(pwd='谢谢大家帮助我。'.encode('gbk'))就不起作用了,提示“ Bad password ”
    Gandum
        6
    Gandum  
    OP
       Apr 2, 2018 via iPad
    @gnaggnoyil 我又研究了一下,zipfile 这个模块似乎本身就有 bug,它无法识别用 Winrar 创建的任何加密 zip,即使密码是纯数字,比如 1234 之类
    gnaggnoyil
        7
    gnaggnoyil  
       Apr 2, 2018
    @Gandum 没用过 zipfile 这个库,抱歉我也没法解答……
    whoami9894
        8
    whoami9894  
       Apr 2, 2018 via Android
    @Gandum python3 下的 zipfile 我用过,没问题,编码用 Unicode
    wisetc
        9
    wisetc  
       Apr 2, 2018 via iPhone
    python3 -m lib2to3 -w <mycode.py>
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   5900 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 63ms · UTC 02:12 · PVG 10:12 · LAX 19:12 · JFK 22:12
    ♥ Do have faith in what you're doing.