问题:
Want to improve this question?想改善这个问题吗? Update the question so it focuses on one problem only by editing this post .更新问题,使其仅通过编辑这篇文章来关注一个问题。
Closed 3 years ago . 3年前关闭。
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.我当然可以自己敲出一些东西,但如果有一些图书馆,我会全神贯注。
A few 'demands':几个“要求”:
- Rather than using
pickle
I would like it to be a straight forward text file that can easily be read and edited.我不希望使用pickle
而是希望它是一个易于阅读和编辑的直接文本文件。 - I want to be able to add dictionary-like data in it, ie, some form of nesting should be supported.我希望能够在其中添加类似字典的数据,即应该支持某种形式的嵌套。
A simplified pseudo example file:一个简化的伪示例文件:
truck:
color: blue
brand: ford
city: new york
cabriolet:
color: black
engine:
cylinders: 8
placement: mid
doors: 2
解决方案:
参考一: https://stackoom.com/question/LD2w参考二: What's the best practice using a settings file in Python? [closed]