python的 json.dumps 中文編碼


python的 json.dumps 中文編碼

  • # -- coding: utf-8 -- 的作用:文件內容以utf-8編碼
  • json.dumps 序列化時對中文默認使用的ascii編碼, print json.dumps(m)輸出unicode編碼的結果
  • 字符串在Python內部的表示是unicode編碼。

  • 因此,在做編碼轉換時,通常需要以unicode作為中間編碼,即先將其他編碼的字符串解碼(decode)成unicode,再從unicode編碼(encode)成另一種編碼。

  • decode的作用是將其他編碼的字符串轉換成unicode編碼

    decode('utf-8')表示將utf-8編碼的字符串轉換成unicode編碼。

  • encode的作用是將unicode編碼轉換成其他編碼的字符串

    encode('gb2312'),表示將unicode編碼的字符串轉換成gb2312編碼。

  • python3中沒有這種問題,所以最簡單的方法是引入__future__模塊,把新版本的特性導入到當前版本

  from __future__ import unicode_literals   print json.dumps(m,ensure_ascii=False) 

參考:https://blog.csdn.net/u014431852/article/details/53058951


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM