【python】:python实现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.三级目录的一些数据修改

假设我们有一个JSON文件data.json,包含三级目录的一些数据:

{
  "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"
        }
      ]
    }
  ]
}

现在,我们想修改第二个用户的第一个宠物的名称为"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()函数读取data.json文件。接着,我们使用data['users'][1]['pets'][0]获取第二个用户的第一个宠物。然后,我们可以修改宠物的名称,通过修改字典pet的键值来实现。最后,我们使用json.dump()函数将修改后的数据保存到原始的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()函数详解:打开指定文件

猜你喜欢

转载自blog.csdn.net/weixin_44322778/article/details/131045935