Python之讀取json資料

2021-10-02 13:58:13 字數 1264 閱讀 2713

從 「檔案」 中載入 json,用json.load

從 「str」 中載入 json,用json.loads

# -*- coding: utf-8 -*-

import json

jsondata =

''# 使用json.loads()方法,轉化為dict或者list型別

# load是從檔案裡面load,loads是從str裡面load

dictd = json.loads(jsondata)

print

(dictd)

print

(type

(dictd))#

print

('--------------'

)# 將字典型別轉化為json格式

aa = json.dumps(dictd)

print

(aa)

print

(type

(aa))#

print

('--------------'

)# 讀取json檔案

with

open

("tjson.json"

,"r"

)as f:

print

(json.load(f)

)print

(type

(f))

print

('--------------'

)# 讀取文字檔案

with

open

("tjson.txt"

,"r")as

file

:print

(json.load(

file))

print

(type

(file

))

輸出結果:

<

class

'dict'

>--

----

----

----

<

class

'str'

>--

----

----

----

<

class

>--

----

----

----

<

class

>

Python讀取Json資料

讀取json資料,實際上是把json格式變成python中字典 列表等格式化的資料,方便索引查詢。可以新建乙個檔案命名為data,然後把下面這段json資料放進去。import json path data f open path,r encoding utf 8 m json.load f jso...

Python 讀取json檔案

建立json檔案 1 8 讀取json檔案 1 import json 2 3 def loadfont 4 f open settings.json encoding utf 8 設定以utf 8解碼模式讀取檔案,encoding引數必須設定,否則預設以gbk模式讀取檔案,當檔案中包含中文時,會報...

python讀取使用json

學習模組之 json 工作中我們通常會遇到需要資料處理json字串資料,python中我們有乙個特別好的工具json 當然還有picle模組 下面我們就來詳細的介紹一下json工具 安裝,載入 pip install json import json簡單使用,注意區別 dict with open ...