Change the encoding method of the python output stream

Source: Internet
Author: User

I recently played BeautifulSoup and wanted to crawl some of my favorite data. In this case, BeautifulSoup converts & nbsp; To '\ xa0' by default, as a result, gbk cannot output the conversion to gbk, but the default encoding method of file output in my system is gbk, so I want to output the file, you need to change the file encoding method. For example, in python 2.4, you can use f1_open('out.html ', 'w', and 'utf-8') to implement this, but what about the new version? After checking for a long time, we finally implemented it. We can use the codecs f‑code.open('out.html ', 'w', 'utf-8') class to print> f, data or f. write (data) is attached with the method to change the encoding method of the standard output stream import codecs, sys old = sys. stdout sys. stdout = codecs. lookup ('iso8859-1') [-1] (sys. stdout)

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.