UTF-8 HTML and CSS files with BOM (and how to remove the BOM with Python)

前端 未结 4 735
失恋的感觉
失恋的感觉 2020-12-03 21:41

First, some background: I\'m developing a web application using Python. All of my (text) files are currently stored in UTF-8 with the BOM. This includes all my HTML template

4条回答
  •  南方客
    南方客 (楼主)
    2020-12-03 22:03

    Since you state:

    All of my (text) files are currently stored in UTF-8 with the BOM

    then use the 'utf-8-sig' codec to decode them:

    >>> s = u'Hello, world!'.encode('utf-8-sig')
    >>> s
    '\xef\xbb\xbfHello, world!'
    >>> s.decode('utf-8-sig')
    u'Hello, world!'
    

    It automatically removes the expected BOM, and works correctly if the BOM is not present as well.

提交回复
热议问题