最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

關(guān)于Python dict存中文字符dumps()的問(wèn)題

 更新時(shí)間:2021年10月28日 17:16:12   作者:Loganer  
這篇文章主要介紹了關(guān)于Python dict存中文字符dumps()的問(wèn)題,本文給大家分享問(wèn)題及解決方案,給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

Background

之前數(shù)據(jù)庫(kù)只區(qū)分了Android,IOS兩個(gè)平臺(tái),游戲上線后現(xiàn)在PM想要區(qū)分國(guó)服,海外服,港臺(tái)服。這幾個(gè)字段從前端那里的接口獲得,code過(guò)程中發(fā)現(xiàn)無(wú)論如何把中文的value丟到dict中存到數(shù)據(jù)庫(kù)中就變成類似這樣**"\u56fd\u670d"**

Solution

1.首先懷疑數(shù)據(jù)庫(kù)編碼問(wèn)題,但看了一下數(shù)據(jù)庫(kù)其他字段有中文格式的,所以要先check數(shù)據(jù)庫(kù)(MySQL)的字符編碼。

在這里插入圖片描述

可以看到明明就TMD是utf-8啊,所以一定不是數(shù)據(jù)庫(kù)層出現(xiàn)的問(wèn)題,回到代碼debug

2.Google一下
這個(gè)問(wèn)題好多都是Python2的解決方案,找到了一個(gè)感覺(jué)靠譜點(diǎn)的

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

博客中的解法,但是我的Python版本是3.9,就會(huì)報(bào)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這個(gè)東東的時(shí)候它不認(rèn)識(shí)'encoding'這個(gè)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時(shí),返回值就可以返回非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)

完美解決問(wèn)題(●ˇ∀ˇ●)

在這里插入圖片描述

到此這篇關(guān)于Python dict存中文字符dumps()的文章就介紹到這了,更多相關(guān)Python dict中文字符dumps()內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 對(duì)Python中列表和數(shù)組的賦值,淺拷貝和深拷貝的實(shí)例講解

    對(duì)Python中列表和數(shù)組的賦值,淺拷貝和深拷貝的實(shí)例講解

    今天小編就為大家分享一篇對(duì)Python中列表和數(shù)組的賦值,淺拷貝和深拷貝的實(shí)例講解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-06-06
  • Python截取字符串的簡(jiǎn)單方法實(shí)例

    Python截取字符串的簡(jiǎn)單方法實(shí)例

    字符串切片也就是截取字符串,取子串,下面這篇文章主要給大家介紹了關(guān)于Python截取字符串的簡(jiǎn)單方法,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2022-06-06
  • Jupyter notebook如何修改平臺(tái)字體

    Jupyter notebook如何修改平臺(tái)字體

    這篇文章主要介紹了Jupyter notebook如何修改平臺(tái)字體,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-05-05
  • pandas?dataframe獲取所有行名稱與列名稱方法示例

    pandas?dataframe獲取所有行名稱與列名稱方法示例

    這篇文章主要給大家介紹了關(guān)于pandas?dataframe獲取所有行名稱與列名稱的相關(guān)資料,Pandas是Python中用于數(shù)據(jù)分析的非常重要的庫(kù),它提供了多種方法來(lái)獲取列名,需要的朋友可以參考下
    2023-09-09
  • python 分離文件名和路徑以及分離文件名和后綴的方法

    python 分離文件名和路徑以及分離文件名和后綴的方法

    今天小編就為大家分享一篇python 分離文件名和路徑以及分離文件名和后綴的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-10-10
  • Scrapy抓取京東商品、豆瓣電影及代碼分享

    Scrapy抓取京東商品、豆瓣電影及代碼分享

    Scrapy,Python開發(fā)的一個(gè)快速、高層次的屏幕抓取和web抓取框架,用于抓取web站點(diǎn)并從頁(yè)面中提取結(jié)構(gòu)化的數(shù)據(jù)。Scrapy用途廣泛,可以用于數(shù)據(jù)挖掘、監(jiān)測(cè)和自動(dòng)化測(cè)試。
    2017-11-11
  • python集合是否可變總結(jié)

    python集合是否可變總結(jié)

    在本篇文章里小編給大家分享了關(guān)于python集合是否可變的相關(guān)知識(shí)點(diǎn)總結(jié),有需要的朋友們學(xué)習(xí)下。
    2019-06-06
  • Python編碼解碼之encode()函數(shù)詳解

    Python編碼解碼之encode()函數(shù)詳解

    這篇文章主要給大家介紹了關(guān)于Python編碼解碼之encode()函數(shù)的相關(guān)資料,Python的encode函數(shù)用于將字符串按照指定的編碼方式進(jìn)行編碼,返回一個(gè)bytes類型的對(duì)象,需要的朋友可以參考下
    2023-07-07
  • python tkinter制作用戶登錄界面的簡(jiǎn)單實(shí)現(xiàn)

    python tkinter制作用戶登錄界面的簡(jiǎn)單實(shí)現(xiàn)

    這篇文章主要介紹了python tkinter制作用戶登錄界面的簡(jiǎn)單實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2021-04-04
  • 在Python中操作列表之List.append()方法的使用

    在Python中操作列表之List.append()方法的使用

    這篇文章主要介紹了在Python中操作列表之List.append()方法的使用,是Python入門學(xué)習(xí)中的基礎(chǔ)知識(shí),需要的朋友可以參考下
    2015-05-05

最新評(píng)論

游戏| 上林县| 阳东县| 海阳市| 通化市| 衢州市| 新龙县| 红原县| 溧水县| 勃利县| 柘城县| 应城市| 盈江县| 孝义市| 清丰县| 重庆市| 延寿县| 开封县| 泰安市| 南靖县| 韶关市| 乌拉特中旗| 仪征市| 三明市| 运城市| 新闻| 攀枝花市| 安义县| 吉首市| 泰宁县| 蓝山县| 阿合奇县| 疏勒县| 安平县| 聊城市| 乳山市| 吉水县| 射洪县| 德庆县| 霸州市| 景谷|