"Little tricks" to kill Python decoding Unicodeencodeerror

Source: Internet
Author: User

Recently began to chew PyQt.

The utility value of this goods is big, it is worth some effort to tinker, toss.

Getting started is a lot of guide books, get started faster. The missing is the high-tech documentation.

There are many things on the web that are more than a QT gourd.

Want to see the pioneers of the lessons, the search for the community, did not find the forum, only to find a mailing list.

Hard to get back a bunch of zip documents, do not want to manually disassemble, write a script to unpack, but encountered trouble.

Mailing list of documents, there will be many natural language encoding, sometimes there will be decoding error: Unicodeencodeerror.

Decoding the problem, is a very persistent python. I'm using 3.4.1, and I haven't really solved it.

Toss a half-day, self-made a way. Speak directly in code:

From gzip import gzipfiledef Special ():    z = Gzipfile ('./mails/2014-august.txt.gz ', ' R ')    f = open ('./0001.txt ', ' W ')    L = z.readlines () for    B in L:        try:            s = B.decode (' GBK ')        except:            s = UTF8 (b)        finally:            s = str (b)        try:            f.write (s)        except:            continue    F.close () #---------------------------- -----------------------def UTF8 (b):    try:        s = B.decode (' utf-8 ')        return s    except:        return str (b)




"Little tricks" to kill Python decoding Unicodeencodeerror

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.