当前位置:   article > 正文

Python保存为json中文Unicode乱码解决json.dump()_python json dump中文

python json dump中文

保存为json中文Unicode乱码:
在这里插入图片描述

可以看到,中文字符没有乱码,只是出现了反斜杠,此时解决方法应考虑是否进行了二次序列化

一、原因1

在dump时加入ensure_ascii=False 即可解决,即json.dump(json_data, f, indent=4, ensure_ascii=False)

二、原因2

json.dumps(data_dict, ensure_ascii=False)已经进行了序列化,在写入文件时,又采用json.dump(json_data, f, indent=4, ensure_ascii=False)方法导致二次序列化,改为f.write(json_data)即可。

import os
import json
import numpy as np

np.set_printoptions(linewidth=400)

data_list = []
result_list = []
is_duplicate = []
for filename in os.listdir("./log_data"):
    print(filename)
    with open("./log_data/" + filename, 'r', encoding="utf-8") as f:
        line = f.readline()
        while line:
            data = line[line.find("【param】") + len("【param】") + 1:line.find("【result】") - 2]
            result = line[line.find("【result】") + len("【result】") + 1:line.find(", 【headers】")]
            data_list.append(data)
            result_list.append(result)
            line = f.readline()

effective_set = set()
print(len(data_list))
for data in data_list:
    try:
        data_dict = json.loads(data)
        trace_id = data_dict.pop("trace_id")
        print(data_dict["match_start_date"])
        effective_set.add(json.dumps(data_dict, ensure_ascii=False))
    except:
        pass
        continue

print(len(effective_set))


n = 1
for json_data in effective_set:
    if n <= 87:
        with open("./log_data2json/" + f'{hash(json_data)}' + ".json", 'w', encoding="utf-8") as f:
            # json.dump(json_data, f, indent=4, ensure_ascii=False)
            f.write(json_data) # 避免二次序列化
        n += 1
print(n)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43

json.dump()与json.dumps()的区别

json.dumps():将Python字典转为字符串:

import json

# 定义 Python 对象
data = {
    "name": "John",
    "age": 30,
    "city": "New York"
}

# 将 Python 对象转换为 JSON 字符串
json_str = json.dumps(data)

# 打印 JSON 字符串
print(json_str)

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

json.dump将Python字典写入文件:

import json

# 定义 Python 对象
data = {
    "name": "John",
    "age": 30,
    "city": "New York"
}

# 打开文件并将 Python 对象写入文件
with open('data.json', 'w') as f:
    json.dump(data, f)

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

json.load()与json.loads()的区别

json.load()读取文件,转化为字典:

with open('s.json', 'r') as f:
    s1 = json.load(f) # dict
  • 1
  • 2

json.loads()将字符串转为字典:

s = '{"name": "wade", "age": 54, "gender": "man"}'
type(json.loads(s))) # dict
  • 1
  • 2

总结

加s的(loads、dumps)操作的是字符串
不加s的(load、dump)操作的是文件,用于读写文件。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/爱喝兽奶帝天荒/article/detail/759600
推荐阅读
相关标签
  

闽ICP备14008679号