- ubuntu12.04环境下使用kvm ioctl接口实现最简单的虚拟机
- Ubuntu 通过无线网络安装Ubuntu Server启动系统后连接无线网络的方法
- 在Ubuntu上搭建网桥的方法
- ubuntu 虚拟机上网方式及相关配置详解
CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.
这篇CFSDN的博客文章Python常用配置文件ini、json、yaml读写总结由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.
本文参考文章,出于学习目的,写本文.
开发项目时,为了维护一些经常需要变更的数据,比如数据库的连接信息、请求的url、测试数据等,需要将这些数据写入配置文件,将数据和代码分离,只需要修改配置文件的参数,就可以快速完成环境的切换或者测试数据的更新,常用的配置文件格式有ini、json、yaml等,下面简单给大家介绍下,python如何读写这几种格式的文件.
ini 即 initialize ,是windows中常用的配置文件格式,结构比较简单,主要由节(section)、键(key)和值(value)组成。每个独立部分称之为section,每个section内,都是key(option)=value形成的键值对.
在python3中,使用自带的configparser库(配置文件解析器)来解析类似于ini这种格式的文件,比如config、conf。 可以看到,ini只有字典一种格式,且全部都是字符串.
1.1 ini的读取删除操作 。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
import
configparser
#使用前,需要创建一个实例
config
=
configparser.configparser()
#读取并打开文件
config.read(
'test.ini'
,encoding
=
'utf-8'
)
#获取sections,返回列表
print
(config.sections())
#[db,data]
#获取sections下的所有options
print
(config.options(
'db'
))
#['user', 'pwd', 'host', 'database', 'port']
#获取指定section下指定的options
print
(config.get(
'db'
,
'user'
))
# root
#获取section中所有键值对
print
(config.items(
'data'
))
#[('admin_user', 'tong'), ('admin_pwd', '123456')]
#删除整个section
config.remove_section(
'data'
)
#删除某个section下的key
config.remove_option(
'db'
,
'host'
)
print
(config.items(
'db'
))
|
1.2 ini 写入操作 。
写入操作可能会比较少 。
1
2
3
4
5
6
7
|
import
configparser
config
=
configparser.configparser()
config[
'url'
]
=
{
'url'
:
'www.baidu.com'
}
#类似于字典操作
with
open
(
'example.ini'
,
'w'
) as configfile:
config.write(configfile)
|
json (javascript object notation) 是一种轻量级的数据交换格式,采用完全独立于语言的文本格式,这些特性使json成为理想的数据交换语言,易于阅读和编写,同时易于机器解析和生成.
1
2
3
4
5
|
{
"name"
:
"smith"
,
"age"
:
30
,
"sex"
:
"男"
}
|
python中使用内置模块json操作json数据,使用json.load()和json.dump方法进行json格式文件读写:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
# 读取json
import
json
with
open
(
'test1.json'
) as f:
a
=
json.load(f)
print
(a)
# 写入json
import
json
dic
=
{
"name"
:
"xiaoming"
,
"age"
:
20
,
"phonenumber"
:
"15555555555"
}
with
open
(
"test2.json"
,
"w"
) as outfile:
json.dump(dic, outfile)
|
有关json更多的介绍请看链接 。
yaml全称yet another markup language(另一种标记语言),它是一种简洁的非标记语言,以数据为中心,使用空格,缩进,分行组织数据,解析成本很低,是非常流行的配置文件语言.
1
2
3
4
5
6
7
8
9
10
11
12
13
|
case1:
info:
title:
"正常登陆"
url: http:
/
/
192.168
.
1.1
/
user
/
login
method:
"post"
json:
username:
"admin"
password:
"123456"
expected:
status_code:
-
200
-
300
content:
"user_id"
|
读取后效果:
yaml支持的数据结构有三种 。
python中使用pyyaml处理yaml格式数据 。
使用前,需要进行安装
1
|
pip install pyyaml
|
3.3 yaml文件读取 。
用python读取yaml文件,先用open方法读取文件数据,再通过load方法转成字典.
1
2
3
4
5
6
|
import
yaml
with
open
(
"testyaml.yaml"
, encoding
=
'utf-8'
) as
file
:
data
=
yaml.safe_load(
file
)
print
(data)
print
(data[
'case1'
][
'json'
])
print
(data[
'case1'
][
'json'
][
'username'
])
|
3.4 yaml文件的写入 。
1
2
3
4
5
6
7
8
9
10
11
12
|
import
yaml
#定义一个字典
content
=
{
'id'
:
1
,
'text'
:
'programming languages'
,
'members'
: [
'java'
,
'python'
,
'python'
,
'c'
,
'go'
,
'shell'
],
'next'
: {
'a'
:
1
,
'b'
:
2
}
}
with
open
(
'test3.yaml'
,
'w'
, encoding
=
'utf-8'
) as
file
:
yaml.dump(content,
file
, default_flow_style
=
false, encoding
=
'utf-8'
, allow_unicode
=
true)
|
以上有三种数据类型,写入文件效果为:
当然手动写也没有问题.
到此这篇关于python常用配置文件ini、json、yaml读写总结的文章就介绍到这了,更多相关python读写ini、json、yaml配置文件内容请搜索我以前的文章或继续浏览下面的相关文章希望大家以后多多支持我! 。
原文链接:https://blog.csdn.net/u011119817/article/details/118581908 。
最后此篇关于Python常用配置文件ini、json、yaml读写总结的文章就讲到这里了,如果你想了解更多关于Python常用配置文件ini、json、yaml读写总结的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
1、定义 设 \(u\) 和 \(v\) 为一张图上的任意两个节点。令 \(c(u, v)\) 为它们之间的边的容量, \(f(u, v)\) 为它们之间的流量,则需要满足以
1、前言 工作中涉及到文件系统,有时候需要判断文件和目录是否存在。我结合apue第四章文件和目录,总结一下如何正确判断文件和目录是否存在,方便以后查询。 2、stat系列函数 stat函数用来
并查集(Union-Find Set): 一种用于管理分组的数据结构。它具备两个操作:(1)查询元素a和元素b是否为同一组 (2) 将元素a和b合并为同一组。 注意:并查集不能将在同一组的元素拆
当下,注解非常流行,以前很长篇的代码,现在基本上一个注解就能搞定。 那,在Mybatis中又有哪些注解呢? Mybatis中的注解基本上都在org.apache.ibatis.annotat
指针操作数组,方法一是p+index,方法二是p[index],第二种方法跟数组访问方法是一样的。 数组引用返回的是数组的第一个元素的指针地址。 可以将指针指向数组的任意元素,然后从那里开始访问
通常部署完php环境后会进行一些安全设置,除了熟悉各种php漏洞外,还可以通过配置php.ini来加固PHP的运行环境,PHP官方也曾经多次修改php.ini的默认设置。 下面对php.ini中一
在JavaScript中,使用typeof可以检测基本数据类型,使用instanceof可以检测引用数据类型。在PHP中,也有检测数据类型的方法,具体如下: 1、输出变量的数据类型(gettype
把图片缓存到本地,在很多场景都会用到,如果只是存储文件信息,那建一个plist文件,或者数据库就能很方便的解决问题,但是如果存储图片到沙盒就没那么方便了。这里简单介绍两种保存图片到沙盒的方法。
(1)需要安装docker容器,在docker容器内安装jenkins,gogs,tomcat。 新建maven项目,添加findbugs plugin。 使用docker
今天主题是实现并发服务器,实现方法有多种版本,先从简单的单进程代码实现到多进程,多线程的实现,最终引入一些高级模块来实现并发TCP服务器。 说到TCP,想起吐槽大会有个段子提到三次握手,也只有程序
如下所示: Ctrl+1或F2快速修复 Ctrl+D快捷删除行 Shift+Enter 快速切换到下一行,在本行的任何位置都可 Ctrl+F11快速运行代码 Alt+上下键 快速移动行(可
JSP是Servlet技术的扩展,本质上是Servlet的简易方式,更强调应用的外表表达。 JSP编译后是”类servlet”。 Servlet和JSP最主要的不同点在于,Servlet的应用逻辑
Java中的Runable,Callable,Future,FutureTask,ExecutorService,Excetor,Excutors,ThreadPoolExcetor在这里对这些关键
读取Java文件到byte数组的三种方法(总结) ? 1
用java实现的数组创建二叉树以及递归先序遍历,递归中序遍历,递归后序遍历,非递归前序遍历,非递归中序遍历,非递归后序遍历,深度优先遍历,广度优先遍历8种遍历方式:
1、简明总结 ASCII(char) 返回字符的ASCII码值 BIT_LENGTH(str) 返回字符串的比特长度 CONCAT(s1,s2…,sn)
java应用服务器(web server),是指运行java程序的web应用服务器软件,不包括nginx、Apache等通用web服务器软件。 一、Tomcat Tomcat是Apache 软件基
事务作为抽象层,允许应用忽略DB 内部一些复杂并发问题和某些硬件、软件故障,简化应用层的处理逻辑:事务中止(transaction abort),而应用仅需重试。对复杂访问模式,事务可大大减少需要考虑
我们在本教程学习了如何描述 XML 文档的结构 我们学习到了如何使用 DTD 来定义一个 XML 文档的合法元素,以及如何在我们的 XML 内部或者作为一个外部引用来声明 DTD 我们学习了如何为
在这个XPath 基础教程中我们讲解了如何在 XML 文档中查找信息 我们可以使用 XPath 的元素和属性在 XML 文档中进行导航 我们也学习了如何使用 XPath 中内建的某些标准函数 如
我是一名优秀的程序员,十分优秀!