問題:
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]