python-3.6 - 如何从主目录中的配置文件中读取值?
问题描述
.gitconfig
在我的 HOME 目录中,格式如下:
cat ~/.gitconfig
[jenkins "https://myjenkins.com/ins1"]
api-key = 12345abcd
[jenkins "https://myjenkins.com/ins2"]
api-key = 1111aaaa
我需要读取 的api-key
值ins1
,下面的代码有效,是否有更好或更 Pythonic 的方式?
>>> from configparser import ConfigParser
>>> from pathlib import Path
>>> config_object = ConfigParser()
>>> home = str(Path.home())
>>> config_object.read(f'{home}/.gitconfig')
>>> ins = "https://myjenkins.com/ins1"
>>> config_object[f'jenkins "{ins}"']['api-key']
'12345abcd'
解决方案
推荐阅读
- tcl - NS 2:跳过重复节点
- apache-spark - 使用正则表达式检查大于零的任何列的多个列
- reactjs - 如何在页面(路由)更改后完成请求之前显示加载指示器?
- oracle - 如何在 quarkus 数据源上指定当前模式?
- android - 如何在android房间加入查询中的内表中进行过滤
- r - 使用 ggplot2 创建均值分组条形图
- python-3.x - 我有我的作业代码。我不知道如何修复错误。我得到一个类型错误
- python-3.x - Ubuntu 上的 Tensorflowhub 负载问题
- python - 熊猫的 qcut 可能四舍五入?
- php - 如何在 php 或其框架中找到“::get”?