zoukankan      html  css  js  c++  java
  • 解决python 保存json到文件时 中文显示16进制编码的问题

    python 2.7

    import codecs
    import json
    with codecs.open('Options.json', 'w', encoding='utf-8') as f:
        json.dump(_data, f, ensure_ascii=False, indent=4, encoding='utf-8')
    
    1. codecs python官方文档中文翻译 使用给定模式打开编码文件,并返回提供透明编码/解码的打包版本。默认文件模式为“r”,表示以读取模式打开文件。
    2. 使用codecs.open(encoding='foo')需要明确的知道Option.json文件的编码格式
    3. indent=4 缩进 4个空格

    python 3

    import json
    with open('Option.json', 'w', encoding='utf-8') as f:
        json.dump(data, f , ensure_ascii=False, indent=4, encoding='utf-8')
    
    1. python 3 中可以直接使用open打开文件并且指定编码格式

    Option.json

    {
    "default": "中文",
    "field": "_display_name",
    "type": "str",
    "len": "255",
    "not_null": "True"
    }

  • 相关阅读:
    js 比较好的博客
    网络相关
    gulp学习笔记--简单入门
    数组和对象的复制
    seajs学习笔记
    art-template引擎模板
    angularJS中的$apply(),$digest(),$watch()
    CMD和AMD
    通过script标签实现跨域
    jQuery基础知识
  • 原文地址:https://www.cnblogs.com/randomlee/p/11345896.html
Copyright © 2011-2022 走看看