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

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

  •  
  •   Gandum · 2018-04-01 23:34:39 +08:00 · 2809 次点击
    这是一个创建于 2187 天前的主题,其中的信息可能已经有所发展或是发生改变。

    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 条回复    2018-04-02 08:56:07 +08:00
    kokutou
        1
    kokutou  
       2018-04-01 23:39:22 +08:00 via Android
    不如看看 python 库:zipfile ?
    gnaggnoyil
        2
    gnaggnoyil  
       2018-04-01 23:43:32 +08:00   ❤️ 1
    8102 年了怎么还有分不清 code point 和 byte 的人.`a.encode("gbk")`不就完了
    Gandum
        3
    Gandum  
    OP
       2018-04-02 00:05:58 +08:00
    @kokutou 这个我考虑过,不过 zipfile 库好像只能在 open 和 extract 时候输密码,我只是想 test 我的压缩包们(事实上我想执行的是 7z t )
    @gnaggnoyil 我之前不知道 os.system 可以输入 bytes,经你启发发现这一点,问题解决,Thank you !
    gnaggnoyil
        4
    gnaggnoyil  
       2018-04-02 00:09:22 +08:00   ❤️ 1
    @Gandum 讲道理你要是知道`errors='replace'`是在干什么的话就不会有这种问题了……
    Gandum
        5
    Gandum  
    OP
       2018-04-02 00:30:14 +08:00
    @gnaggnoyil 恩,我会去仔细看看的!不过这位朋友还请留步一下,请问在 zipfile 里面怎么解决这个问题呢?
    这里 ZipFile.extractall(pwd='谢谢大家帮助我。'.encode('gbk'))就不起作用了,提示“ Bad password ”
    Gandum
        6
    Gandum  
    OP
       2018-04-02 01:35:56 +08:00 via iPad
    @gnaggnoyil 我又研究了一下,zipfile 这个模块似乎本身就有 bug,它无法识别用 Winrar 创建的任何加密 zip,即使密码是纯数字,比如 1234 之类
    gnaggnoyil
        7
    gnaggnoyil  
       2018-04-02 07:39:04 +08:00
    @Gandum 没用过 zipfile 这个库,抱歉我也没法解答……
    whoami9894
        8
    whoami9894  
       2018-04-02 08:14:55 +08:00 via Android
    @Gandum python3 下的 zipfile 我用过,没问题,编码用 Unicode
    wisetc
        9
    wisetc  
       2018-04-02 08:56:07 +08:00 via iPhone
    python3 -m lib2to3 -w <mycode.py>
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5504 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 07:57 · PVG 15:57 · LAX 00:57 · JFK 03:57
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.