安装bson包, sudo pip install bson

示例

# 解决编码问题
import sys
reload(sys)
sys.setdefaultencoding('utf8')

# 导入bson模块
import bson

# 通过rb的方式读取文件
bson_file = open('./xunfeng/db/Config.bson', 'rb')

# loads接收的是字符串
data = bson.loads(bson_file.read())

如果不进行编码会报错UnicodeDecodeError: 'ascii' codec can't decode byte 0xe5 in position 0: ordinal not in range(128)

主要是因为bson文件中可能包含中文.

Python2的编码问题真恶心.

相关文章:

  • 2022-12-23
  • 2021-04-09
  • 2021-11-27
  • 2022-12-23
  • 2021-12-06
  • 2021-09-13
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-01-28
  • 2021-09-05
相关资源
相似解决方案