python 字符串编码 ,区别 utf-8 和utf-8-sig

时间:2019-03-15
本文章向大家介绍python 字符串编码 ,区别 utf-8 和utf-8-sig,主要包括python 字符串编码 ,区别 utf-8 和utf-8-sig使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

As UTF-8 is an 8-bit encoding no BOM is required and anyU+FEFF character in the decoded Unicode string (even if it’s the firstcharacter) is treated as a ZERO WIDTH NO-BREAK SPACE.

UTF-8以字节为编码单元,它的字节顺序在所有系统中都是一様的,没有字节序的问题,也因此它实际上并不需要BOM(“ByteOrder Mark”)。

但是UTF-8 with BOM即utf-8-sig需要提供BOM。
---------------------
原文:https://blog.csdn.net/vernice/article/details/46873169

  • 摘要:问题描述:json.loads(text,encoding='utf8')报UnexpectedUTF-8BOM(decodeusingutf-8-sig)错误,将encoding改为'utf-8-sig'仍然报错。原因分析:text包含BOM字符解决方案:将BOM头去掉

  • 问题描述:

    json.loads(text,encoding='utf8') 报Unexpected UTF-8 BOM (decode using utf-8-sig)错误,将encoding改为'utf-8-sig'仍然报错。

    原因分析:

    text包含BOM字符

    解决方案:

    将BOM头去掉,代码如下:

 if text.startswith(u'/ufeff'):
    text = text.encode('utf8')[3:].decode('utf8')


链接:https://www.jianshu.com/p/f94b3fc04f5b