欧美性猛交xxx嘿人猛交_又色又爽又高潮免费观看_精品国产一区二区三区久久影院_青娱乐极品视觉盛宴国产视频

技術頻道導航
HTML/CSS
.NET技術
IIS技術
PHP技術
Js/JQuery
Photoshop
Fireworks
服務器技術
操作系統
網站運營

贊助商

分類目錄

贊助商

最新文章

搜索

5個示例介紹Python如何讀、寫操作JSON文件

作者:admin    時間:2021-12-27 9:5:47    瀏覽:

本文通過5個示例,介紹Python如何讀、寫操作JSON文件。

JSON 是將數據表示為文件中的文本的輕量級格式,其語法借用了用于創建 JavaScript 對象的語法。海量數據轉換為JSON格式,便于各種編程語言的處理和傳輸到其他節點。它是處理 API 調用時最常用的請求和響應格式。

 5個Python示例讀寫操作JSON文件

JSON 格式接受的數據類型

JSON 鍵必須是字符串。大多數語言隱式地將任何其他數據類型轉換為字符串。JSON 值可以是字符串、布爾值、整數、浮點數、另一個 JSON 對象或 JSON 數組。

以下是有效 JSON 文件 (data.json) 的示例:

{
  "name": "Jason",
  "age": 21,
  "address": {
    "street": "1 Main Street",
    "city": "Los Angeles",
    "zipcode": 90001
  },
  "married": false
}

這里,“address”鍵的值是另一個 JSON。鍵“married”是具有布爾類型的值。

請注意,JSON 不支持注釋。此處給出的所有代碼片段也與 python 3 兼容。

Python 中最接近 JSON 的數據類型是字典。Python 使用名為 json 的模塊(一個內置的 Python 庫)支持字典、JSON 和字符串的相互轉換。

Python 讀取 JSON 文件

JSON 文件通常具有擴展名 .json。但是,python 支持解析任何文件擴展名,直到它包含有效的 JSON。要讀取 json 文件并將數據填充到字典中,請使用 json.load()。該函數將文件對象作為參數。以下是說明 json.load() 使用的示例。

示例代碼片段:

import json

with open("data.json") as f:
    p = json.load(f)
    print(p, type(p))
    print(p["name"], "is", "married" if p["married"] else "not married")

上面例子的輸出:

{'name': 'Jason', 'age': 21, 'address': {'street': '1 Main Street', 'city': 'Los Angeles', 'zipcode': 90001}, 'married': False} <class 'dict'>
Jason is not married

這里 f 是文件對象。json.load() 返回一個字典。

上面的代碼片段使用 python if else 格式。

Python將字符串或字節數組讀入字典

Json 模塊允許用戶將 json 以字符串、字節或字節數組的形式解析為字典。這是使用函數 json.loads() 完成的。請注意函數名稱與前面描述的函數的相似之處。load() 需要一個文件對象作為參數,而 load() 需要一個字符串作為參數。前者讀取文件并隱式調用后者,即loads()

示例代碼片段:

import json

with open("data.json") as f:
    file_data = f.read()
p = json.loads(file_data)
print(p, type(p))
print(p["name"], "is", "married" if p["married"] else "not married")

上面例子的輸出:

{'name': 'Jason', 'age': 21, 'address': {'street': '1 Main Street', 'city': 'Los Angeles', 'zipcode': 90001}, 'married': False} <class 'dict'>
Jason is not married

Python將字典寫入 JSON 文件

只能將字符串、字節數組或字節寫入文件。因此,字典必須轉換為 JSON 格式的字符串才能寫入 json 文件。這是使用函數 json.dump() 完成的。該函數將字典和文件對象作為參數。它不返回任何東西。以下是相同的說明。

示例代碼片段:

import json

json_dict = {
    "name": "Jason",
    "age": 21,
    "address": {
        "street": "1 Main Street",
        "city": "Los Angeles",
        "zipcode": 90001
    },
    "married": False
}

print("Type of json_dict:", type(json_dict))

with open("data.json", "w") as f:
    json.dump(json_dict, f, indent=4)

上面例子的輸出:

Type of json_dict: <class 'dict'>

data.json
{
    "name": "Jason",
    "age": 21,
    "address": {
        "street": "1 Main Street",
        "city": "Los Angeles",
        "zipcode": 90001
    },
    "married": false
}

請注意,json 文件使用制表符格式化為 4 個空格。這是由于 json.dump() 函數中的“indent”參數而生效的。

Python將 JSON 格式的字符串寫入文件

在前面的示例中,將字典解析為字符串并寫入文件是在一個語句中使用一個函數完成的。在這里,這一步分為兩步:首先將字典轉換為字符串,保存到變量中,然后寫入文件。

示例python代碼片段:

import json

json_dict = {
    "name": "Jason",
    "age": 21,
    "address": {
        "street": "1 Main Street",
        "city": "Los Angeles",
        "zipcode": 90001
    },
    "married": False
}

print("Type of json_dict:", type(json_dict))

with open("data.json", "w") as f:
    v = json.dumps(json_dict)
    print("v has value", v, "and is of type", type(v))
    f.write(v)

上面例子的輸出:

Type of json_dict: <class 'dict'>
v has value {"name": "Jason", "age": 21, "address": {"street": "1 Main Street", "city": "Los Angeles", "zipcode": 90001}, "married": false} and is of type <class 'str'>

data.json
{"name": "Jason", "age": 21, "address": {"street": "1 Main Street", "city": "Los Angeles", "zipcode": 90001}, "married": false}

請注意,data.json 文件未格式化。這是因為 json.dumps() 函數中省略了參數“indent”。

總結

本文通過5個示例,介紹了Python如何讀、寫操作JSON文件。

標簽: Python  
相關文章
    x