Why can an imported CSV have an invisible character in its first header?
The utf-8-sig decoder recognizes and skips an optional UTF-8 byte-order mark at the beginning of the stream.
Python example and interpretation
Reading a signed header as plain UTF-8 adds U+FEFF to the id key. Handle the leading signature rather than removing every such character from the data.
import codecs
raw=codecs.BOM_UTF8+b'id'
[ord(raw.decode()[0]),raw.decode('utf-8-sig')]Result [65279, 'id']
中文
为什么导入 CSV 后首列名称前有一个不可见字符?
utf-8-sig 解码会识别并跳过流开头可选的 UTF-8 字节顺序标记。
Python 算例与适用条件
带标记的 id 表头若用普通 UTF-8 解码,其键会多出 U+FEFF。只处理开头的签名,正文中同一字符的意义需单独判断。
import codecs
raw=codecs.BOM_UTF8+b'id'
[ord(raw.decode()[0]),raw.decode('utf-8-sig')]结果 [65279, 'id']