BOM in server response screws up json parsing

后端 未结 3 1728
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-31 18:55

I\'m trying to write a Python script that posts some JSON to a web server and gets some JSON back. I patched together a few different examples on StackOverflow, and I think

3条回答
  •  青春惊慌失措
    2020-12-31 19:18

    You should probably yell at whoever's running this service, because a BOM on UTF-8 text makes no sense. The BOM exists to disambiguate byte order, and UTF-8 is defined as being little-endian.

    That said, ideally you should decode bytes before doing anything else with them. Luckily, Python has a codec that recognizes and removes the BOM: utf-8-sig.

    >>> '\xef\xbb\xbffoo'.decode('utf-8-sig')
    u'foo'
    

    So you just need:

    data = json.loads(response.decode('utf-8-sig'))
    

提交回复
热议问题