Python string to list appears \ufeff workaround

Source: Internet
Author: User

  When you learn that Python reads a file from a file and converts a string from a file into a list, you find that the file header has a \ufeff character.

This problem was encountered before, the same is the Internet search for a half-day to solve, at that time just to solve the problem is over, but today encountered the same problem, know there is such a solution, but how to do is not to remember. The Ancients cloud, good memory than bad writing, a bit right. Go to the chase.

 A few conceptual things  

Anscii:









STR). Like some other coding methods gb2312, GB18030, Big5 and UTF are the same, just coded differently.

A text file created under Windows with a text editor that, if saved in Unicode format such as UTF-8, adds a BOM ID to the file header (the first character). Specific removal methods look at the following code
1 #filename:example.py2 #Conding=utf-83 4f = open ("News.txt","R", encoding='Utf-8')5File =F.read ()6File_list = File.split (",")7 Print(file_list)8 9File_list2 = File.encode ('Utf-8'). Decode ('Utf-8-sig')Ten Print(FILE_LIST2) One  A  - #printing results are as follows -['\ufeff Xinhua News Agency, Beijing, February 8, February 8'] theBEIJING, February 8 (Xinhua) February 8

Do not understand a lot, and slowly accumulate.

Python string to list appears \ufeff workaround

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.