Python Learning path: file with auto-close + character transcoding

Source: Internet
Author: User

The WITH statement enables automatic file shutdown:

1 ImportSYS2 " "3 With open ("Yesterday2", "R", encoding= "Utf-8") as F: #自动关闭并释放文件资源4 For line in F:5 print (line)6 " "7With open ("Yesterday2","R", encoding="Utf-8") as F,8Open"Yesterday2","R", encoding="Utf-8") as F2:9      forLineinchF:Ten         Print(line)

Character transcoding: https://www.cnblogs.com/jxzheng/p/5186490.html

Https://www.cnblogs.com/nulige/p/6063999.html

1s ="Hello" #default Unicode2S_TO_GBK =s.encode ("GBK")#Unicode transcoding into GBK3 Print(S_TO_GBK)4 5 Print(S_to_gbk.decode ("GBK"))#Transcoding from GBK to Unicode6 7 Print(S.encode ("GBK"))8 Print(S.encode ("Utf-8"))9 Print(S.encode ("Utf-8"). Decode ("Utf-8"). Encode ("gb2312"))Ten  One ImportSYS A Print(Sys.getdefaultencoding ())#Fetch system default encoding

Python Learning path: file with auto-close + character transcoding

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.