Skip to content
Navigation Menu
Sign in
Products
Platform
GitHub
Repository hosting, code review, and collaboration
Copilot
AI coding agent for tasks, projects, and automated work
Build & Ship
Actions
CI/CD and automation
Code Quality
Quality analysis, fixes, and enforcement
Codespaces
Cloud development environments
Secure
Code Security
Vulnerability detection and remediation
Secret Protection
Secret scanning and leak prevention
All Features
Developers
Learn
Documentation
Training
Certifications
Build
Build with Copilot
Customize Copilot
Integrations
Explore
Trending repos
GitHub Sponsors
Developer articles
Changelog
Changelog
Your source for the latest features, improvements, and fixes across GitHub products, all in one place.
Businesses
By Stage
Enterprises
Team
Startups
Nonprofits
By Industry
Healthcare
Financial services
Manufacturing
Government
See all industries
Evaluate GitHub
Why GitHub
Customer Stories
Business insights
Trust Center
Add-ons
GitHub Advanced Security
Enterprise-grade code security
Copilot for business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Resources
Learn & Connect
Documentation
Resource center
Blog
Events & webinars
Community Forum
Customer Stories
Integrations
Support
Customer support
Training
GitHub Status
Company
About
Partners
Changelog
GitHub Universe
GitHub Universe
The event for developers, by developers — talks, workshops, and what’s next for GitHub.
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
hangx969
python-operation-scripts
Repository navigation
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
More
items
Files
Expand file tree
main
Breadcrumbs
python-operation-scripts
/
python-config-file-operation
/
json-config-management.py
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
45 lines (36 loc) · 1.53 KB
main
Breadcrumbs
python-operation-scripts
/
python-config-file-operation
/
json-config-management.py
Copy path
Top
File metadata and controls
Code
Blame
45 lines (36 loc) · 1.53 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
"""
JSON配置文件管理脚本
功能描述:
该脚本演示了如何使用Python的json模块对JSON格式的配置文件进行
读取、修改和保存操作。提供了一个完整的JSON配置文件管理流程,
适用于应用程序配置管理、系统参数调整等场景。
技术实现:
- json.load(): 从文件中读取JSON数据并反序列化为Python对象
- json.dump(): 将Python对象序列化并写入JSON文件
- 字典操作: 使用键值对方式修改配置项
- 文件上下文管理: 使用with语句确保文件正确关闭
- indent参数: 设置JSON输出格式缩进,提高可读性
配置文件操作流程:
1. 打开并读取JSON配置文件
2. 使用json.load()将JSON内容反序列化为Python字典
3. 通过字典键值访问和修改配置项
4. 支持嵌套字典的深层字段修改
5. 使用json.dump()将修改后的字典序列化回JSON格式
6. 保存到原文件或新文件中
"""
import json
# 有一个json配置文件,需要修改其中的字段:
# 1. 读取json文件
# 2. 反序列化成python对象
# 3. 修改字段
# 4. 序列化到文件中
# 读取json文件并反序列化成字典
with open('python-manuscripts/config.json', 'r') as f:
config = json.load(f)
# 字典赋值的方法来修改字段
config['version'] = '1.0.1'
config['logging']['level'] = 'DEBUG'
config['notifications']['email']['username'] = 'test_user'
# 序列化到json文件
with open('python-manuscripts/config.json', 'w') as f:
json.dump(config, f, indent=4)
You can’t perform that action at this time.