3

在 python 2.x 中,我使用过

"shift-jis".decode('shift-jis').encode('utf-8')

但在 python 3.x 中没有更多的 str.decode()。python 3.x中的等效代码是什么?

更新 :

更详细:

python2函数是

def unzip(file, dir):
    zips = zipfile.ZipFile(file)
    for info in zips.infolist():
        info.filename = info.filename.decode('shift-jis').encode('utf-8')
        zips.extract(info,dir)

        print(info, filename)

这个函数的等效 python3 代码是什么?

4

2 回答 2

5

对于您更新的问题:

def unzip(file, directory): # dir is a keyword
    with zipfile.ZipFile(file, mode='r') as zips:
        zips.printdir()
        zips.extractall(directory)

.

>>> b'\x82\xb3'.decode('shiftjis')
'さ'
>>> b'\x82\xb3'.decode('shift-jis')
'さ'
>>> b'\x82\xb3'.decode('shift_jis')
'さ'
>>> '日本語'.encode('shiftjis')
b'\x93\xfa\x96{\x8c\xea'
>>> b'\x93\xfa\x96{\x8c\xea'.decode('shiftjis')
'日本語'

和读取文件时:

with open('shiftjis.txt', 'r', encoding='shiftjis') as file:
    # do something with it

阅读更多:http ://docs.python.org/3.3/library/io.html#io-base-classes

一个不太理智的版本:

with open('shiftjis.txt', 'rb') as file:
    string = file.read().decode('shift-jis')
于 2013-01-12T20:18:09.513 回答
0

我需要自己做这件事,而天真的做法是:

def unzip(file, dir):
    zips = zipfile.ZipFile(file)
    for info in zips.infolist():
        info.filename = info.filename.encode("cp437").decode("shift-jis")
    print("Extracting: " + info.filename.encode(sys.stdout.encoding,errors='replace').decode(sys.stdout.encoding))
    zips.extract(info,dir)
    print("")

ZipFile似乎在内部将所有文件名视为 DOS(代码页 437)。与 Python 2 不同,Python 3 在内部将所有字符串存储为某种 UTF。因此,我们将文件名转换为字节数组,并将原始字节字符串解码为 shift-JIS 以得到最终文件名。

print行做类似的事情,但默认编码stdout和返回。这可以防止在 Windows 上发生错误,因为它的终端几乎从不支持 Unicode。(如果是,则名称应正确显示。)

这适用于几个 zip 文件,直到 bam...

Traceback (most recent call last):
  File "jp\j-unzip.py", line 73, in <module>
    unzip(archname,archpath)
  File "jp\j-unzip.py", line 68, in unzip
    info.filename = info.filename.encode("cp437").decode("shift-jis")
UnicodeDecodeError: 'shift_jis' codec can't decode byte 0x8f in position 28: illegal multibyte sequence

奖励内容!弄清楚这一点花了一些时间,但问题是一些有效的 shift-JIS 字符包含反斜杠,ZipFile 将其转换为正斜杠!例如,十在 shift-JIS 中编码为8F 5C. 这被转换为8F 2F非法序列。如果发生错误,以下(可能过于复杂)代码会检查这种情况,并尝试修复它。但也许还有其他字符发生这种情况,并且序列是有效的,所以你得到了错误的字符而不是错误。:(

def convert_filename(inname):
    err_ctr=0
    keep_going = True

    trans_filename = bytearray(inname.encode("cp437"))

    while keep_going:
        keep_going = False

        try:
            outname = trans_filename.decode("shift-jis")
        except UnicodeDecodeError as e:
            keep_going = True

            if e.args[4]=="illegal multibyte sequence":
                p0, p1 = e.args[2], e.args[3]
                print("Trying to fix encoding error at positions " + str(p0) +", "+ str(p1) + " caused by shift-jis sequence " + hex(trans_filename[p0]) +", "+  hex(trans_filename[p1]) )

                if (trans_filename[p0]>127 and trans_filename[p1] == 0x2f):
                    trans_filename[p1] = 0x5c
                else:
                    print("Don't know how to fix this error. Quitting. :(")
                    raise e

                err_ctr = err_ctr + 1
                print("This is error #" + str(err_ctr) + " for this filename.")
            else:
                raise e

        if err_ctr>50:
            print("More than 50 iterations. Are we stuck in an endless loop? Quitting...")
            sys.exit(1)

    return outname

def unzip(file, dir):
    zips = zipfile.ZipFile(file)
    for info in zips.infolist():
        info.filename = convert_filename(info.filename)
    print("Extracting: " + info.filename.encode(sys.stdout.encoding,errors='replace').decode(sys.stdout.encoding))
    zips.extract(info,dir)
    print("")
于 2014-01-24T04:21:16.750 回答