Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

character encoding conversion in python 3

In python 2.x, I have used

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

but there is no more str.decode() in python 3.x. what is the equivalent code in python 3.x?

Update :

More specific:

The python2 function is

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)

What is the equivalent python3 code for this function?

like image 968
kReoSo Avatar asked Jun 16 '26 14:06

kReoSo


1 Answers

To your updated question:

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')
'日本語'

and when reading files:

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

Read more: http://docs.python.org/3.3/library/io.html#i-o-base-classes

a less saner version:

with open('shiftjis.txt', 'rb') as file:
    string = file.read().decode('shift-jis')
like image 168
mflatischler Avatar answered Jun 18 '26 02:06

mflatischler



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!