关于Python dict存中文字符dumps()的问题

作者:Loganer 时间:2022-08-22 17:53:17 

Background

之前数据库只区分了Android,IOS两个平台,游戏上线后现在PM想要区分国服,海外服,港台服。这几个字段从前端那里的接口获得,code过程中发现无论如何把中文的value丢到dict中存到数据库中就变成类似这样**"\u56fd\u670d"**

Solution

1.首先怀疑数据库编码问题,但看了一下数据库其他字段有中文格式的,所以要先check数据库(MySQL)的字符编码。

关于Python dict存中文字符dumps()的问题

可以看到明明就TMD是utf-8啊,所以一定不是数据库层出现的问题,回到代码debug

2.Google一下
这个问题好多都是Python2的解决方案,找到了一个感觉靠谱点的


dict1 = {'name':'张三'}
print(json.dumps(dict1,encoding='utf-8',ensure_ascii=False))

博客中的解法,但是我的Python版本是3.9,就会报Error如下


Exception in thread Thread-1:
Traceback (most recent call last):
 File "/usr/local/python3/lib/python3.9/threading.py", line 950, in _bootstrap_inner
   self.run()
 File "/usr/local/python3/lib/python3.9/threading.py", line 888, in run
   self._target(*self._args, **self._kwargs)
 File "/home/dapan_ext/project_table.py", line 91, in http_request
   self.get_data(project_response_data)
 File "/home/dapan_ext/project_table.py", line 115, in get_data
   json.dumps(dict_1, encoding='utf-8', ensure_ascii=False)
 File "/usr/local/python3/lib/python3.9/json/__init__.py", line 234, in dumps
   return cls(
TypeError: __init__() got an unexpected keyword argument 'encoding'

意思就是:在__init__json这个东东的时候它不认识'encoding'这个argument。

那就翻阅源码康康->->:


def dumps(obj, *, skipkeys=False, ensure_ascii=True, check_circular=True,
       allow_nan=True, cls=None, indent=None, separators=None,
       default=None, sort_keys=False, **kw):
   """Serialize ``obj`` to a JSON formatted ``str``.

If ``skipkeys`` is true then ``dict`` keys that are not basic types
   (``str``, ``int``, ``float``, ``bool``, ``None``) will be skipped
   instead of raising a ``TypeError``.

If ``ensure_ascii`` is false, then the return value can contain non-ASCII
   characters if they appear in strings contained in ``obj``. Otherwise, all
   such characters are escaped in JSON strings.

If ``check_circular`` is false, then the circular reference check
   for container types will be skipped and a circular reference will
   result in an ``OverflowError`` (or worse).

If ``allow_nan`` is false, then it will be a ``ValueError`` to
   serialize out of range ``float`` values (``nan``, ``inf``, ``-inf``) in
   strict compliance of the JSON specification, instead of using the
   JavaScript equivalents (``NaN``, ``Infinity``, ``-Infinity``).

If ``indent`` is a non-negative integer, then JSON array elements and
   object members will be pretty-printed with that indent level. An indent
   level of 0 will only insert newlines. ``None`` is the most compact
   representation.

If specified, ``separators`` should be an ``(item_separator, key_separator)``
   tuple.  The default is ``(', ', ': ')`` if *indent* is ``None`` and
   ``(',', ': ')`` otherwise.  To get the most compact JSON representation,
   you should specify ``(',', ':')`` to eliminate whitespace.

``default(obj)`` is a function that should return a serializable version
   of obj or raise TypeError. The default simply raises TypeError.

If *sort_keys* is true (default: ``False``), then the output of
   dictionaries will be sorted by key.

To use a custom ``JSONEncoder`` subclass (e.g. one that overrides the
   ``.default()`` method to serialize additional types), specify it with
   the ``cls`` kwarg; otherwise ``JSONEncoder`` is used.

"""
   # cached encoder
   if (not skipkeys and ensure_ascii and
       check_circular and allow_nan and
       cls is None and indent is None and separators is None and
       default is None and not sort_keys and not kw):
       return _default_encoder.encode(obj)
   if cls is None:
       cls = JSONEncoder
   return cls(
       skipkeys=skipkeys, ensure_ascii=ensure_ascii,
       check_circular=check_circular, allow_nan=allow_nan, indent=indent,
       separators=separators, default=default, sort_keys=sort_keys,
       **kw).encode(obj)

注意到这里:

If ``ensure_ascii`` is false, then the return value can contain non-ASCII
    characters if they appear in strings contained in ``obj``. Otherwise, all
    such characters are escaped in JSON strings.

意思就是:
ensure_ascii置为false时,返回值就可以返回非ASCII编码的字符,这岂不正是我们需要的,Got it!

回去改代码:


server_name = str(related['name'])
# print(server_name)
dict_1 = {'appKey': related['appKey'], 'client': related['client'], 'name': server_name}
crasheye.append(dict_1)
crasheyes = json.dumps(crasheye, ensure_ascii=False)

完美解决问题(●ˇ∀ˇ●)

关于Python dict存中文字符dumps()的问题

来源:https://blog.csdn.net/wyh1618/article/details/121013434

标签:Python,dict,中文字符,dumps
0
投稿

猜你喜欢

  • Spring MVC+MyBatis+MySQL实现分页功能实例

    2024-01-26 22:23:23
  • Python 安装第三方库 pip install 安装慢安装不上的解决办法

    2023-02-23 13:43:45
  • TorchVision Transforms API目标检测实例语义分割视频类

    2022-12-05 14:24:56
  • Python必备技巧之字典(Dictionary)详解

    2022-02-12 23:01:13
  • mysql 5.7.14 安装配置方法图文教程

    2024-01-12 14:01:18
  • 在Dreamweaver MX中应用“占位图形”

    2009-07-10 13:16:00
  • 教你利用Selenium+python自动化来解决pip使用异常

    2022-11-17 18:49:08
  • CSS实现HTML元素透明的那些事

    2010-02-01 12:34:00
  • MySQL explain获取查询指令信息原理及实例

    2024-01-22 15:25:54
  • python实现雪花飘落效果实例讲解

    2022-08-29 07:31:55
  • pywinauto自动化测试使用经验

    2022-12-21 02:36:10
  • python中二分查找法的实现方法

    2023-02-02 16:19:16
  • 从云数据迁移服务看MySQL大表抽取模式的原理解析

    2024-01-24 01:45:41
  • bootstrap table列和表头对不齐的解决方法

    2023-08-24 16:30:08
  • selenium与xpath之获取指定位置的元素的实现

    2022-01-14 12:02:35
  • 深入浅析mybatis oracle BLOB类型字段保存与读取

    2024-01-15 02:21:26
  • Python Django的安装配置教程图文详解

    2023-06-29 08:17:12
  • Python命令行参数解析包argparse的使用详解

    2021-04-01 01:58:52
  • SQL Server查找表名或列名中包含空格的表和列实例代码

    2024-01-17 03:15:33
  • 验证sql保留字工具

    2008-05-15 12:34:00
  • asp之家 网络编程 m.aspxhome.com