欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python實現(xiàn)json文件的增刪改操作方法

 更新時間:2023年06月06日 11:35:52   作者:時間之里  
這篇文章主要介紹了python實現(xiàn)json文件的增刪改操作,本文通過示例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下

python實現(xiàn)json文件的增刪改

1.原始JSON文件

以下是一個例子,假設原始的JSON文件內容如下:

{
  "name": "Alice",
  "age": 30,
  "address": {
    "city": "Beijing",
    "street": "Main Street"
  },
  "hobbies": ["reading", "swimming", "traveling"]
}

2.修改操作

修改操作:

import json
# 加載JSON文件
with open('example.json', 'r') as f:
    data = json.load(f)
# 修改姓名
data['name'] = 'Bob'
# 修改年齡
data['age'] = 35
# 修改住址
data['address']['city'] = 'Shanghai'
data['address']['street'] = 'West Street'
# 保存修改后的JSON文件
with open('example.json', 'w') as f:
    json.dump(data, f, indent=2)    # 保持2個縮進

3.刪除操作

刪除操作:

import json
# 加載JSON文件
with open('example.json', 'r') as f:
    data = json.load(f)
# 刪除愛好中的一個
data['hobbies'].remove('reading')
# 刪除整個address
del data['address']
# 保存修改后的JSON文件
with open('example.json', 'w') as f:
    json.dump(data, f, indent=2)

4.新增操作

新增操作:

import json
# 加載JSON文件
with open('example.json', 'r') as f:
    data = json.load(f)
# 新增一個愛好
data['hobbies'].append('writing')
# 新增一個電話號碼
data['phone'] = '13888888888'
# 保存修改后的JSON文件
with open('example.json', 'w') as f:
    json.dump(data, f, indent=2)

5.三級目錄的一些數(shù)據(jù)修改

假設我們有一個JSON文件data.json,包含三級目錄的一些數(shù)據(jù):

{
  "users": [
    {
      "name": "John",
      "age": 28,
      "pets": [
        {
          "name": "Fluffy",
          "type": "cat"
        },
        {
          "name": "Fido",
          "type": "dog"
        }
      ]
    },
    {
      "name": "Mary",
      "age": 33,
      "pets": [
        {
          "name": "Rex",
          "type": "dog"
        },
        {
          "name": "Whiskers",
          "type": "cat"
        },
        {
          "name": "Buddy",
          "type": "dog"
        }
      ]
    }
  ]
}

現(xiàn)在,我們想修改第二個用戶的第一個寵物的名稱為"Smokey"。可以使用以下代碼:

import json
# 讀取JSON文件
with open('data.json', 'r') as f:
    data = json.load(f)
# 獲取第二個用戶的第一個寵物
pet = data['users'][1]['pets'][0]
# 修改寵物名稱
pet['name'] = 'Smokey'
# 保存修改后的JSON文件
with open('data.json', 'w') as f:
    json.dump(data, f, indent=2)

運行以上代碼后,data.json文件的內容將會變成:

{
  "users": [
    {
      "name": "John",
      "age": 28,
      "pets": [
        {
          "name": "Fluffy",
          "type": "cat"
        },
        {
          "name": "Fido",
          "type": "dog"
        }
      ]
    },
    {
      "name": "Mary",
      "age": 33,
      "pets": [
        {
          "name": "Smokey",
          "type": "dog"
        },
        {
          "name": "Whiskers",
          "type": "cat"
        },
        {
          "name": "Buddy",
          "type": "dog"
        }
      ]
    }
  ]
}

在這個示例代碼中,我們首先使用json.load()函數(shù)讀取data.json文件。接著,我們使用data['users'][1]['pets'][0]獲取第二個用戶的第一個寵物。然后,我們可以修改寵物的名稱,通過修改字典pet的鍵值來實現(xiàn)。最后,我們使用json.dump()函數(shù)將修改后的數(shù)據(jù)保存到原始的JSON文件中。

【err】 1.open()讀取文件提示:“UnicodeDecodeError”

“UnicodeDecodeError: ‘gbk’ codec can’t decode byte 0x80 in position 205: illegal multibyte sequence”

解決辦法1.

FILE_OBJECT= open(‘order.log’,‘r’, encoding=‘UTF-8’)
解決辦法2.

FILE_OBJECT= open(‘order.log’,‘rb’)

Python open()函數(shù)詳解:打開指定文件

到此這篇關于python實現(xiàn)json文件的增刪改操作的文章就介紹到這了,更多相關python json文件的增刪改內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

最新評論