如何用Python读取不同格式的配置文件(ini、yaml、xml)?

2026-05-27 00:431阅读0评论SEO资源
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计1523个文字,预计阅读时间需要7分钟。

如何用Python读取不同格式的配置文件(ini、yaml、xml)?

零、前言在Python代码中配置文件是必不可少的。配置文件中包含了程序运行所需的各种参数和设置。常见的配置文件格式有ini、yaml、xml、properties、txt、py等。下面以ini文件为例,简要介绍其格式。

一、ini文件格式

1.语法结构

ini文件通常由多个节(section)组成,每个节包含一系列键值对。节与键值对之间用方括号和等号分隔。

[注释内容][节名称]键1=值1键2=值2

2. 内容说明- 注释内容:位于文件开头,用于说明文件用途或参数含义。- 节名称:方括号内的内容,用于区分不同配置。- 键值对:由键和值组成,键与值之间用等号连接。

二、示例以下是一个简单的ini文件示例,用于配置百度搜索的URL:

[注释]百度搜索配置文件

[url]baidu=https://www.baidu.com/s?wd=

零、前言

python代码中配置文件是必不可少的内容。常见的配置文件格式有很多中:ini、yaml、xml、properties、txt、py等。

一、ini文件

1.1 ini文件的格式

; 注释内容

[url] ; section名称
baidu = www.jb51.net
port = 80

[email]
sender = 'xxx@qq.com'

注意section的名称不可以重复,注释用分号开头。

1.2 读取 configparser

python自带的configparser模块可以读取.ini文件,注意:在python2中是ConfigParser

创建文件的时候,只需要在pychrame中创建一个扩展名为.ini的文件即可。

import configparser file = 'config.ini' # 创建配置文件对象 con = configparser.ConfigParser() # 读取文件 con.read(file, encoding='utf-8') # 获取所有section sections = con.sections() # ['url', 'email'] # 获取特定section items = con.items('url') # 返回结果为元组 # [('baidu','www.jb51.net'),('port', '80')] # 数字也默认读取为字符串 # 可以通过dict方法转换为字典 items = dict(items)

二、yaml配置文件

2.1 yaml文件格式

yaml文件是用来方便读写的一种格式。它实质上是一种通用的数据串行话格式。

它的基本语法如下:

大小写敏感

缩进表示层级关系

缩进时不允许使用Tab,仅允许空格

空格的多少不重要,关键是相同层级的元素要对齐

#表示注释,#后面的字符都会被忽略

yaml支持的数据格式包括:

字典
数组
纯量:单个的,不可再次分割的值

2.1.2 对象

对象是一组组的键值对,使用冒号表示结构

url: www.jb51.net log: file_name: test.log backup_count: 5

yaml也允许另外一种写法,将所有的键值对写成一个行内对象

log: {file_name: test.log, backup_count: 5}

2.1.3 数组

一组横线开头的行,组成一个数组。

- cat
- Dog
- Goldfish

转换成python对象是

['cat', 'Dog', 'Goldfish']

数组也可以采用行内写法:

animal: [cat, dog]

转行成python对象是

{'animal': ['cat', 'dog']}

2.1.4 纯量

纯量是最基本,不可分割的值。

数字和字符串直接书写即可:

number: 12.30
name: zhangsan

布尔值用true和false表示

isSet: true
flag: false

null用~表示

parent: ~

yaml允许用两个感叹号表示强制转换

如何用Python读取不同格式的配置文件(ini、yaml、xml)?

e: !!str 123
f: !!str true

2.1.5 引用

锚点&和别名*,可以用来引用

defaults: &defaults adapter: postgres host: localhost development: databases: myapp_deveploment <<: *defaults test: databases: myapp_test <<: *defaults

等同于以下代码

defaults: adapter: postgres host: localhost development: databases: myapp_deveploment adapter: postgres host: localhost test: databases: myapp_test adapter: postgres host: localhost

&用来建立锚点(defaults),<<表示合并到当前数据,*用来引用锚点

下面是另外一个例子:

- &abc st
- cat
- dog
- *abc

转换成python代码是:

['st', 'cat', 'dog', 'st']

2.2 yaml文件的读取

读取yaml文件需要先安装相应模块。

pip install yaml

yaml文件内容如下:

url: www.baidu.com email: send: xxx@qq.com port: 25 --- url: www.sina.com.cn

读取代码如下:

# coding:utf-8 import yaml # 获取yaml文件路径 yamlPath = 'config.yaml' with open(yamlPath,'rb') as f: # yaml文件通过---分节,多个节组合成一个列表 date = yaml.safe_load_all(f) # salf_load_all方法得到的是一个迭代器,需要使用list()方法转换为列表 print(list(date))

三、xml配置文件读取

xml文件内容如下:

<collection shelf="New Arrivals"> <movie title="Enemy Behind"> <type>War, Thriller</type> <format>DVD</format> <year>2003</year> <rating>PG</rating> <stars>10</stars> <description>Talk about a US-Japan war</description> </movie> <movie title="Transformers"> <type>Anime, Science Fiction</type> <format>DVD</format> <year>1989</year> <rating>R</rating> <stars>8</stars> <description>A schientific fiction</description> </movie> <movie title="Trigun"> <type>Anime, Action</type> <format>DVD</format> <episodes>4</episodes> <rating>PG</rating> <stars>10</stars> <description>Vash the Stampede!</description> </movie> <movie title="Ishtar"> <type>Comedy</type> <format>VHS</format> <rating>PG</rating> <stars>2</stars> <description>Viewable boredom</description> </movie> </collection>

读取代码如下:

# coding=utf-8 import xml.dom.minidom from xml.dom.minidom import parse DOMTree = parse('config.xml') collection = DOMTree.documentElement if collection.hasAttribute("shelf"): print("Root element : %s" % collection.getAttribute("shelf")) # 在集合中获取所有电影 movies = collection.getElementsByTagName("movie") # 打印每部电影的详细信息 for movie in movies: print("*****Movie*****") if movie.hasAttribute("title"): print("Title: %s" % movie.getAttribute("title")) type = movie.getElementsByTagName('type')[0] print("Type: %s" % type.childNodes[0].data) format = movie.getElementsByTagName('format')[0] print("Format: %s" % format.childNodes[0].data) rating = movie.getElementsByTagName('rating')[0] print("Rating: %s" % rating.childNodes[0].data) description = movie.getElementsByTagName('description')[0] print("Description: %s" % description.childNodes[0].data)

以上这篇python读取配置文件方式(ini、yaml、xml)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持易盾网络。

本文共计1523个文字,预计阅读时间需要7分钟。

如何用Python读取不同格式的配置文件(ini、yaml、xml)?

零、前言在Python代码中配置文件是必不可少的。配置文件中包含了程序运行所需的各种参数和设置。常见的配置文件格式有ini、yaml、xml、properties、txt、py等。下面以ini文件为例,简要介绍其格式。

一、ini文件格式

1.语法结构

ini文件通常由多个节(section)组成,每个节包含一系列键值对。节与键值对之间用方括号和等号分隔。

[注释内容][节名称]键1=值1键2=值2

2. 内容说明- 注释内容:位于文件开头,用于说明文件用途或参数含义。- 节名称:方括号内的内容,用于区分不同配置。- 键值对:由键和值组成,键与值之间用等号连接。

二、示例以下是一个简单的ini文件示例,用于配置百度搜索的URL:

[注释]百度搜索配置文件

[url]baidu=https://www.baidu.com/s?wd=

零、前言

python代码中配置文件是必不可少的内容。常见的配置文件格式有很多中:ini、yaml、xml、properties、txt、py等。

一、ini文件

1.1 ini文件的格式

; 注释内容

[url] ; section名称
baidu = www.jb51.net
port = 80

[email]
sender = 'xxx@qq.com'

注意section的名称不可以重复,注释用分号开头。

1.2 读取 configparser

python自带的configparser模块可以读取.ini文件,注意:在python2中是ConfigParser

创建文件的时候,只需要在pychrame中创建一个扩展名为.ini的文件即可。

import configparser file = 'config.ini' # 创建配置文件对象 con = configparser.ConfigParser() # 读取文件 con.read(file, encoding='utf-8') # 获取所有section sections = con.sections() # ['url', 'email'] # 获取特定section items = con.items('url') # 返回结果为元组 # [('baidu','www.jb51.net'),('port', '80')] # 数字也默认读取为字符串 # 可以通过dict方法转换为字典 items = dict(items)

二、yaml配置文件

2.1 yaml文件格式

yaml文件是用来方便读写的一种格式。它实质上是一种通用的数据串行话格式。

它的基本语法如下:

大小写敏感

缩进表示层级关系

缩进时不允许使用Tab,仅允许空格

空格的多少不重要,关键是相同层级的元素要对齐

#表示注释,#后面的字符都会被忽略

yaml支持的数据格式包括:

字典
数组
纯量:单个的,不可再次分割的值

2.1.2 对象

对象是一组组的键值对,使用冒号表示结构

url: www.jb51.net log: file_name: test.log backup_count: 5

yaml也允许另外一种写法,将所有的键值对写成一个行内对象

log: {file_name: test.log, backup_count: 5}

2.1.3 数组

一组横线开头的行,组成一个数组。

- cat
- Dog
- Goldfish

转换成python对象是

['cat', 'Dog', 'Goldfish']

数组也可以采用行内写法:

animal: [cat, dog]

转行成python对象是

{'animal': ['cat', 'dog']}

2.1.4 纯量

纯量是最基本,不可分割的值。

数字和字符串直接书写即可:

number: 12.30
name: zhangsan

布尔值用true和false表示

isSet: true
flag: false

null用~表示

parent: ~

yaml允许用两个感叹号表示强制转换

如何用Python读取不同格式的配置文件(ini、yaml、xml)?

e: !!str 123
f: !!str true

2.1.5 引用

锚点&和别名*,可以用来引用

defaults: &defaults adapter: postgres host: localhost development: databases: myapp_deveploment <<: *defaults test: databases: myapp_test <<: *defaults

等同于以下代码

defaults: adapter: postgres host: localhost development: databases: myapp_deveploment adapter: postgres host: localhost test: databases: myapp_test adapter: postgres host: localhost

&用来建立锚点(defaults),<<表示合并到当前数据,*用来引用锚点

下面是另外一个例子:

- &abc st
- cat
- dog
- *abc

转换成python代码是:

['st', 'cat', 'dog', 'st']

2.2 yaml文件的读取

读取yaml文件需要先安装相应模块。

pip install yaml

yaml文件内容如下:

url: www.baidu.com email: send: xxx@qq.com port: 25 --- url: www.sina.com.cn

读取代码如下:

# coding:utf-8 import yaml # 获取yaml文件路径 yamlPath = 'config.yaml' with open(yamlPath,'rb') as f: # yaml文件通过---分节,多个节组合成一个列表 date = yaml.safe_load_all(f) # salf_load_all方法得到的是一个迭代器,需要使用list()方法转换为列表 print(list(date))

三、xml配置文件读取

xml文件内容如下:

<collection shelf="New Arrivals"> <movie title="Enemy Behind"> <type>War, Thriller</type> <format>DVD</format> <year>2003</year> <rating>PG</rating> <stars>10</stars> <description>Talk about a US-Japan war</description> </movie> <movie title="Transformers"> <type>Anime, Science Fiction</type> <format>DVD</format> <year>1989</year> <rating>R</rating> <stars>8</stars> <description>A schientific fiction</description> </movie> <movie title="Trigun"> <type>Anime, Action</type> <format>DVD</format> <episodes>4</episodes> <rating>PG</rating> <stars>10</stars> <description>Vash the Stampede!</description> </movie> <movie title="Ishtar"> <type>Comedy</type> <format>VHS</format> <rating>PG</rating> <stars>2</stars> <description>Viewable boredom</description> </movie> </collection>

读取代码如下:

# coding=utf-8 import xml.dom.minidom from xml.dom.minidom import parse DOMTree = parse('config.xml') collection = DOMTree.documentElement if collection.hasAttribute("shelf"): print("Root element : %s" % collection.getAttribute("shelf")) # 在集合中获取所有电影 movies = collection.getElementsByTagName("movie") # 打印每部电影的详细信息 for movie in movies: print("*****Movie*****") if movie.hasAttribute("title"): print("Title: %s" % movie.getAttribute("title")) type = movie.getElementsByTagName('type')[0] print("Type: %s" % type.childNodes[0].data) format = movie.getElementsByTagName('format')[0] print("Format: %s" % format.childNodes[0].data) rating = movie.getElementsByTagName('rating')[0] print("Rating: %s" % rating.childNodes[0].data) description = movie.getElementsByTagName('description')[0] print("Description: %s" % description.childNodes[0].data)

以上这篇python读取配置文件方式(ini、yaml、xml)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持易盾网络。