加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

解决python 保存json到文件时 中文显示16进制编码的问题

发布时间:2020-12-20 11:00:58 所属栏目:Python 来源:网络整理
导读:python 2.7 import codecsimport jsonwith codecs.open('Options.json','w',encoding='utf-8') as f: json.dump(_data,f,ensure_ascii=False,indent=4,encoding='utf-8') codecs python官方文档中文翻译 使用给定模式打开编码文件,并返回提供透明编码/解码

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',encoding='utf-8') as f:
    json.dump(data,encoding='utf-8')
  1. python 3 中可以直接使用open打开文件并且指定编码格式

Option.json

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读