python_note
  • Introduction
  • My Python
    • Anaconda
    • argparse
    • datetime
    • json
    • logging
    • numpy
    • open
    • openCC
    • pandas & csv
    • Socket & SocketServer
    • re
    • yaml
    • smtp
    • 物件操作
    • unittest
    • 線程
    • prettytable
    • IO
    • pycurl
    • sys
    • pickle
    • auto-python-to-exe
    • cython
    • nuitka
  • Crawler
    • Urllib & Requests
      • User-agent
      • Percent-Encoding
      • mail code
    • Selenium
    • TCP & UDP
    • 控制字符(control character)
  • Web Development
    • Flask
      • RESTful api
      • Template
      • blueprint
    • Django
      • 環境佈署(windows)
    • 檢查Port
    • Apache
    • 使用者行為
    • jQuery
    • 壓力測試
    • DataTable
    • Bootstrap
    • CSS
    • JavaScript
    • Chart.js
  • Deep Learning
    • Keras 設定
    • RNN
    • LSTM
  • Test
    • T-Test
  • 資料結構
    • Hash
    • 時間複雜度
  • NLP
    • N-gram
    • CKIP
    • 中文轉數字
    • CRF
    • Mutual Information
    • 模糊比對
  • Linebot
    • Heroku
    • 圖文選單
    • channel
  • Linux
    • 常用指令
    • shell script
    • sshfs
    • ssh
    • nodejs & npm
    • debug
  • GCP
    • app engine
    • ssh(gcp)
    • gsutil
    • brabrabra
    • Load Balancer
    • k8s
  • Database
    • mysql
    • elasticsearch
      • Query
      • Backup and Restore
      • elasticdump
      • es2csv
      • ELK
    • mongodb
      • install
      • authentication
      • pymongo
    • sql server
  • go
    • Swarm
  • Docker
    • Kitematic
    • Dockerfile
    • Swarm
  • Git
  • 其他
    • USB軟體保護
    • Windows效能監視器
  • Blockchain
Powered by GitBook
On this page
  • dumps 與 dump,loads與load
  • 檔案輸出使用dump與write的結果差異
  • 檔案輸出編碼設定

Was this helpful?

  1. My Python

json

dumps 與 dump,loads與load

不加s --> 可用於檔案輸入輸出相關操作

加s --> 可用於程式中json物件操作

dump(s)時可使用indent參數讓json內容方便閱讀,但也會增加使用空間

參數ensure_ascii=False --> 顯示中文

檔案輸出使用dump與write的結果差異

主要差異在於可讀性

dump會詳細標記出符號,如:\"、\n,並以" "包住整個{內容}

write則直接顯示符號以及換行效果

但兩者皆可以load讀取操作

檔案輸出編碼設定

用codecs解決編碼問題 (python2)

import json,codecs
content='123'
with codecs.open('filepath','w',encoding='utf8') as output:
    json.dump(content,output)
PreviousdatetimeNextlogging

Last updated 5 years ago

Was this helpful?

文件參考