Using python to remove Windows text file header BOM code

  • 2020-04-02 09:44:27
  • OfStack

Question:
Create or edit a text file in Windows and save it with a BOM in the header.
Using FTP to upload to Linux, the first line of execution is an error.
The following method can remove the BOM head, the need of friends can refer to.


import codecs
data = open("Test.txt").read()
if data[:3] == codecs.BOM_UTF8:
 data = data[3:]
print data.decode("utf-8")

Note: the beginning part of the file is 0xEF 0xBB 0xBF for BOM


Related articles: