The example in this article describes the solution to the garbled problem when Python traverses the output name of the zip file. Share to everyone for your reference. Specifically as follows:
Windows in the use of python2.7 traverse the zip file after the output file name information, console printing Chinese and some punctuation appears garbled. Check it out. The Windows encoding for the Cp936,print () function is given to the system to process the printing, so encode it in advance to the code that Windows recognizes.
This print garbled also appears in the form of print (MyList) (MyList is a python list type variable, print (Mylist[2]) is not garbled, strange)
The code is as follows: (in the. py file, first add #-*-coding:utf-8–*-in the file header)
Import ZipFile
def listzipfilesinfo (path):
z=zipfile. ZipFile (path, ' R ')
try: for
filename in z.namelist ():
bytes=z.read (filename)
print (' file:%s size:% S '% (Unicode (filename, ' cp936 '). Decode (' Utf-8 '), Len (bytes))
finally:
z.close ()
Note: Decode can be removed
I hope this article will help you with your Python programming.