本文翻译自:What's the best practice using a settings file in Python? [closed]

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: bluebrand: ford
city: new york
cabriolet:color: blackengine:cylinders: 8placement: middoors: 2

#1楼

参考:https://stackoom.com/question/LD2w/在Python中使用设置文件的最佳做法是什么-关闭


#2楼

I Found this the most useful and easy to use https://wiki.python.org/moin/ConfigParserExamples 我发现这是最有用和易于使用的https://wiki.python.org/moin/ConfigParserExamples

You just create a "myfile.ini" like: 您只需创建一个“ myfile.ini”,例如:

[SectionOne]
Status: Single
Name: Derek
Value: Yes
Age: 30
Single: True[SectionTwo]
FavoriteColor=Green
[SectionThree]
FamilyName: Johnson[Others]
Route: 66

And retrieve the data like: 并像这样检索数据:

>>> import ConfigParser
>>> Config = ConfigParser.ConfigParser()
>>> Config
<ConfigParser.ConfigParser instance at 0x00BA9B20>
>>> Config.read("myfile.ini")
['c:\\tomorrow.ini']
>>> Config.sections()
['Others', 'SectionThree', 'SectionOne', 'SectionTwo']
>>> Config.options('SectionOne')
['Status', 'Name', 'Value', 'Age', 'Single']
>>> Config.get('SectionOne', 'Status')
'Single'

#3楼

Yaml and Json are the simplest and most commonly used file formats to store settings/config. Yaml和Json是存储设置/配置的最简单和最常用的文件格式。 PyYaml can be used to parse yaml. PyYaml可用于解析yaml。 Json is already part of python from 2.5. Json已经从2.5开始成为python的一部分。 Yaml is a superset of Json. Yaml是Json的超集。 Json will solve most uses cases except multi line strings where escaping is required. Json将解决大多数使用情况,但需要转义的多行字符串除外。 Yaml takes care of these cases too. Yaml也会处理这些情况。

>>> import json
>>> config = {'handler' : 'adminhandler.py', 'timeoutsec' : 5 }
>>> json.dump(config, open('/tmp/config.json', 'w'))
>>> json.load(open('/tmp/config.json'))
{u'handler': u'adminhandler.py', u'timeoutsec': 5}

#4楼

You can have a regular Python module, say config.py, like this: 您可以有一个常规的Python模块,例如config.py,如下所示:

truck = dict(color = 'blue',brand = 'ford',
)
city = 'new york'
cabriolet = dict(color = 'black',engine = dict(cylinders = 8,placement = 'mid',),doors = 2,
)

and use it like this: 并像这样使用它:

import config
print config.truck['color']

#5楼

The sample config you provided is actually valid YAML . 您提供的示例配置实际上是有效的YAML 。 In fact, YAML meets all of your demands, is implemented in a large number of languages, and is extremely human friendly. 实际上,YAML可以满足您的所有需求,并以多种语言实现,并且非常人性化。 I would highly recommend you use it. 我强烈建议您使用它。 The PyYAML project provides a nice python module, that implements YAML. PyYAML项目提供了一个不错的python模块,该模块实现了YAML。

To use the yaml module is extremely simple: 使用yaml模块非常简单:

import yaml
config = yaml.safe_load(open("path/to/config.yml"))

在Python中使用设置文件的最佳做法是什么? [关闭]相关推荐

  1. python语言做法_在Python中使用设置文件的最佳做法是什么?

    我发现这是最有用和易于使用的 https://wiki.python.org/moin/ConfigParserExamples 您只需创建一个" myfile.ini",例如: ...

  2. python怎么输入代码-python中如何设置代码自动提示

    第一步:打开pycharm,如下图所示: 第二步:File→Power Save Mode,把下面如图所示的勾去掉: 第三步:去掉勾后,不再使用省电模式,新建一个 python文件,输入需要输入的单词 ...

  3. python中os操作文件及文件路径

    python中os操作文件及文件路径实例汇总     1 . python获取文件上一级目录:取文件所在目录的上一级目录 os.path.abspath(os.path.join(os.path.di ...

  4. Python中的File(文件)操作

    Python中的File(文件)操作 针对磁盘中的文件的读写.文件I/O I 输入(input) O输出(Output) 文件操作步骤:1.打开文件 2.读写文件 3.关闭文件 写入文件的操作:(把大 ...

  5. 详解Python中的File(文件)操作

    目录 Python中的File(文件)操作 写入文件的操作: 读取文件的操作: 一.文件操作相关函数 1. open() 打开文件 2. seek() 设置文件指针的位置 3. write() 写入内 ...

  6. python中如何打开文件选择框

    Python中如何打开文件选择框 1.代码: import win32ui dlg = win32ui.CreateFileDialog(1) # 1表示打开文件对话框 dlg.SetOFNIniti ...

  7. 如何在Python中解析YAML文件

    如何在Python中解析YAML文件? #1楼 不依赖C标头的最简单,最纯净的方法是PyYaml( 文档 ): #!/usr/bin/env pythonimport yamlwith open(&q ...

  8. Python 中的Pyc文件

    Python 中的Pyc文件 分类: Python 2011-04-22 16:52 4055人阅读 评论(0) 收藏 举报 pythonimport虚拟机跨平台googlebyte 首先我申明,我不 ...

  9. 【Python】用于在 Python 中处理 PDF 文件的 PyPDF2 库

    作者 | megha152 编译 | Flin 来源 | analyticsvidhya 介绍 PDF 代表便携式文档格式.它使用 .pdf 扩展名.这种类型的文件主要用于共享目的.它们不能被修改,从 ...

最新文章

  1. python窗体设置italic_007萝卜头学python:Python GUI 之Tkinter
  2. 入选广州粤菜食材生产基地 农业大健康·林裕豪:从玉农业品牌化
  3. tebluea 仪表板如何联动_报告厅音响设备和辅助设备.doc
  4. 第四篇:基本数据类型及用法(1)
  5. PostgreSQL的执行计划分析
  6. 【TFS 2017 CI/CD系列 - 01】-- Agent篇
  7. Hive里的分区、分桶、视图和索引再谈
  8. matlab电力系统建模仿真实验,电力系统建模及仿真课程设计
  9. linux启动和grub修复
  10. 《互联网的那些事之时代》第三回:滟滟随波千万里,何处春江无月明
  11. MySQL 事务四大特性和事务隔离级别
  12. 条件概率公式、全概率公式以及贝叶斯公式
  13. 自监督学习在计算机视觉中的应用
  14. 2009 year English Inprovement for IT developments
  15. 单组学的多变量分析|1.PCA和PLS-DA
  16. 第八周编程题在线测试
  17. 北京队“接触风波”受罚背后:CBA职业化不断进步
  18. -- 40、查询选修“张三“老师所授课程的学生中,成绩最高的学生信息及其成绩
  19. elasticsearch 基础 —— Query String
  20. python初体验-布尔类型转换

热门文章

  1. Azure Linux 虚机上配置 RAID 的常见问题及解决方案
  2. 【Go语言】【16】GO语言的并发
  3. 算法模板——线段树6(二维线段树:区域加法+区域求和)(求助phile)
  4. 团队成员的分应该怎么分?
  5. 29th, Dec 2011 求人不如求己
  6. 11月热门下载资源TOP100强力推荐!
  7. JDBC——连接数据库
  8. 第八篇:ZTree操作总结
  9. 批处理-删除环境变量
  10. 钩子教程 - 原理(一)