Remove BOM headers in python and remove bom headers in python
In the window environment, the saved text document will contain three characters in the header 0xEF 0xBB 0xBF. These three characters may affect the reading of the text and cause garbled characters, here we will record how to avoid it.
First, we found that text files directly saved as ASCII do not contain the BOM header. The verification steps are as follows:
1. Create an ASCII txt file
2.
# Coding = utf-8import codecsdata = open ("xxx.txt"). read () if data [: 3] = codecs. BOM_UTF8: print "contain BOM" print data
It is found that the ASCII code file header does not include BOM.
3. Save the file as the UFT-8 format, run again, you can find that contains BOM header, the output is also garbled.
Solution
# Coding = utf-8import codecsdata = open ("xxx.txt"). read () if data [: 3] = codecs. BOM_UTF8: data = data [3:] print data