马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
x
一、 配置文件
1.1 ini
官方-configparser
config.ini文件如下:
- [url] ; section名称
- baidu = https://www.zalou.cn
- port = 80
- [email]
- sender = ‘xxx@qq.com’
复制代码- import configparser
- # 读取
- file = 'config.ini'
- # 创建配置文件对象
- con = configparser.ConfigParser()
- # 读取文件
- con.read(file, encoding='utf-8')
- # 取值, 把con当做嵌套字典来用即可
- con["url"]
- con["url"]["port"]
- # 获取所有section
- sections = con.sections() # ['url', 'email']
- # 获取特定section
- items = con.items('url') # 返回结果为元组 # [('baidu','https://www.zalou.cn'),('port', '80')] # 数字也默认读取为字符串
- # 可以通过dict方法转换为字典
- items = dict(items)
- # 写入
- import configparser
- config = configparser.ConfigParser()
- config['DEFAULT'] = {'ServerAliveInterval': '45',
- 'Compression': 'yes',
- 'CompressionLevel': '9'}
- config['bitbucket.org'] = {}
- config['bitbucket.org']['User'] = 'hg'
- config['topsecret.server.com'] = {}
- topsecret = config['topsecret.server.com']
- topsecret['Port'] = '50022' # mutates the parser
- topsecret['ForwardX11'] = 'no' # same here
- config['DEFAULT']['ForwardX11'] = 'yes'
- with open('example.ini', 'w') as configfile:
- config.write(configfile)
复制代码 特殊符号读取
注意,若配置中有特殊符号,如;大概#在ini的section后是用于解释的,在=后可直接读取进配置中
嵌套配置读取
- [url] ; section名称
- [url.search];搜索网址
- name = https://www.zalou.cn
- port = 80
- [url.news];搜索网址
- name = https://www.sina.com
- port = 80
复制代码- import configparser
- con = configparser.ConfigParser()
- con.read("test.ini", encoding="utf-8")
- print(con.sections()) # ['url', 'url.search', 'url.news']
- print(con["url.news"]["name"]) # https://www.sina.com
复制代码 1.2 yaml
官方-pyyaml
Python读写yaml文件
Python基础条记1-Python读写yaml文件(使用PyYAML库)
- # 写入,字典数据
- import yaml
- desired_caps = {
- 'platformName': 'Android哈哈哈', # 移动设备系统IOS或Android
- 'platformVersion': '7.1.2', # Android手机系统版本号
- 'deviceName': '852', # 手机唯一设备号
- 'app': 'C:\\Users\\wangli\\Desktop\\kbgz-v5.9.0-debug.apk', # APP文件路径
- 'appPackage': 'com', # APP包名
- 'appActivity': 'cui.setup.SplashActivity', # 设置启动的Activity
- 'noReset': 'True', # 每次运行不重新安装APP
- 'unicodeKeyboard': 'True', # 是否使用unicode键盘输入,在输入中文字符和unicode字符时设置为true
- 'resetKeyboard': 'True', # 隐藏键盘
- 'autoGrantPermissions': 'True',
- 'autoAcceptAlerts': ["python", "c++", "java"],
- 'chromeOptions': {'androidProcess': 'com.tencent.mm:tools'}
- }
- with open("test1.yaml", "w", encoding="utf-8") as f:
- yaml.dump(desired_caps, f, Dumper=yaml.RoundTripDumper)
- # 写入-列表数据
- list_data = ['python', 'java', 'c++', 'C#', {'androidProcess': 'com.tencent.mm:tools'}, ["python", "c++", "java"]]
- with open("test2.yaml", "w", encoding="utf-8") as f:
- yaml.dump(list_data , f, Dumper=yaml.RoundTripDumper)
- # 读取-得到字典
- with open('test1.yaml', 'r', encoding='utf-8') as f:
- conf=yaml.load(f.read(),Loader=yaml.Loader) # dict, 和desired_caps 一致
- # 读取-得到列表
- with open('test2.yaml', 'r', encoding='utf-8') as f:
- conf=yaml.load(f.read(),Loader=yaml.Loader) # list, 和list_data 一致
复制代码 test1.yaml写入后如下
- deviceName: '852'
- unicodeKeyboard: 'True'
- autoAcceptAlerts:
- - python
- - c++
- - java
- autoGrantPermissions: 'True'
- platformVersion: 7.1.2
- platformName: "Android\u54C8\u54C8\u54C8"
- app: C:\Users\wangli\Desktop\kbgz-v5.9.0-debug.apk
- appPackage: com
- chromeOptions:
- androidProcess: com.tencent.mm:tools
- appActivity: cui.setup.SplashActivity
- noReset: 'True'
- resetKeyboard: 'True'
复制代码 test2.yaml写入后如下
- - python
- - java
- - c++
- - C#
- - androidProcess: com.tencent.mm:tools
- - - python
- - c++
- - java
复制代码 1.3 动态配置读取
用Dynaconf举行Python项目标配置管理
官方-dynaconf
dyanconf的最大特点是用一套代码,从不同的配置数据存储方式中读取配置,例如python配置文件、系统环境变量、redis、ini文件、json文件等等。具体用法参考上面第一个毗连,这里不再赘述。
二、 数据文件
不想展开讨论,以下仅列举可读取的方式毗连。
1.1 csv
方案1:csv库
方案2:pandas
1.2 excel
方案1:
官方教程openpyxl
Python3使用openpyxl读取和写入excel
方案2:pandas
1.3 xml
xml虽然常被用作配置文件,但他本身的计划是用来存储数据的。
方案1:
xml.dom
xml.dom.minidom
xml.sax
xml.sax.hanldler
xml.sax.reader
xml.etree.ElementTree
方案2:
pandas
xml文件先容:
XML文件详解(具体易明确)
XML——根本语法及使用规则
1.4 json
json 虽然也被用作配置文件,但更多情况是用来传递数据。
- import json
- py_data= {
- 'no' : 1,
- 'name' : 'Runoob',
- 'url' : 'http://www.runoob.com'
- }
- # 写入
- with open('data.json', 'w') as fh:
- json_str = json.dumps(py_data)
- fh.write(json_str)
- with open('data.json', 'w') as fh:
- json.dump(a, fh)
- # 读取
- with open("./data.json", "r") as f:
- content = json.load(f)
- print(type(content)) # <class 'dict'>
- print(content)
复制代码 方案1:json模块
方案2:pandas
方案3:dynaconf
其他参考
python读取配置文件方式(ini、yaml、xml)
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |