我有一个带有很多参数的命令行脚本.我现在已经到了一个论点太多的地步,我也想有一些字典形式的论点.
I have a command line script that I run with a lot of arguments. I have now come to a point where I have too many arguments, and I want to have some arguments in dictionary form too.
所以为了简化事情,我想用设置文件来运行脚本.我真的不知道要使用哪些库来解析文件.这样做的最佳做法是什么?我当然可以自己敲出一些东西,但是如果有一些图书馆可以解决这个问题,我会全力以赴.
So in order to simplify things I would like to run the script with a settings file instead. I don't really know what libraries to use for the parsing of the file. What's the best practice for doing this? I could of course hammer something out myself, but if there is some library for this, I'm all ears.
一些要求":
pickle,我希望它是一个易于阅读和编辑的简单文本文件.pickle I would like it to be a straight forward text file that can easily be read and edited.一个简化的伪示例文件:
A simplified pseudo example file:
truck:
color: blue
brand: ford
city: new york
cabriolet:
color: black
engine:
cylinders: 8
placement: mid
doors: 2
你可以有一个普通的 Python 模块,比如 config.py,像这样:
You can have a regular Python module, say config.py, like this:
truck = dict(
color = 'blue',
brand = 'ford',
)
city = 'new york'
cabriolet = dict(
color = 'black',
engine = dict(
cylinders = 8,
placement = 'mid',
),
doors = 2,
)
并像这样使用它:
import config
print(config.truck['color'])
这篇关于在 Python 中使用设置文件的最佳做法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
python:不同包下同名的两个模块和类python: Two modules and classes with the same name under different packages(python:不同包下同名的两个模块和类)
配置 Python 以使用站点包的其他位置Configuring Python to use additional locations for site-packages(配置 Python 以使用站点包的其他位置)
如何在不重复导入顶级名称的情况下构造python包How to structure python packages without repeating top level name for import(如何在不重复导入顶级名称的情况下构造python包)
在 OpenShift 上安装 python 包Install python packages on OpenShift(在 OpenShift 上安装 python 包)
如何刷新 sys.path?How to refresh sys.path?(如何刷新 sys.path?)
分发带有已编译动态共享库的 Python 包Distribute a Python package with a compiled dynamic shared library(分发带有已编译动态共享库的 Python 包)