配置文件中的键值对的列表在python中 [英] Configuration file with list of key-value pairs in python

查看:186
本文介绍了配置文件中的键值对的列表在python中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个python脚本,它分析一组错误消息,并检查每个消息,如果它匹配某个模式(正则表达式)为了分组这些消息。例如,文件x不存在和文件y不存在将匹配文件。*不存在,并且被视为两次出现未找到文件类别。



随着模式和类别数量的增长,我想把这些夫妇正则表达式/显示字符串在配置文件中,基本上是字典序列化有些类别。



我希望这个文件可以手动编辑,所以我丢弃任何形式的二进制序列化,也不想诉诸于xml序列化以避免字符转义的问题(&<>等等)。



你有什么可能是一个好方法完成这个?



更新:感谢Daren Thomas和Federico Ramponi,但我不能有一个外部python文件可能任意代码。

解决方案

您有两个不错的选择:


  1. Python标准配置文件格式
    ,使用 ConfigParser

  2. YAML 使用像 PyYAML

文件看起来像带有 [sections] key:value key = value 对。此格式的优点是:




  • 不需要第三方库

  • 简单,熟悉的文件格式。



YAML的不同之处在于它被设计为人类友好的数据序列化格式,而不是专门为配置而设计。它是非常可读的,并给了你几种不同的方式来表示相同的数据。对于您的问题,您可以创建一个如下所示的YAML文件:

 文件。*不存在:文件未找到
用户。*未找到:授权错误

或者这样:

  {file。*不存在:文件未找到,
用户。*未找到:授权错误}

使用PyYAML不容易:

  import yaml 

errors = yaml.load(open('my.yaml'))

此时 errors 是一个具有预期格式的Python字典。 YAML能够代表多个字典:如果你喜欢一个对的列表,请使用以下格式:

   -  
- 文件*不存在
- 找不到文件
-
- 用户。未找到
- 授权错误
pre>

  [[文件。*不存在,文件未找到],
[用户。*未找到,授权错误]]

将调用 yaml.load 时将生成一个列表列表。



YAML的一个优点是,它会将现有的硬编码数据导出到一个文件以创建初始版本,而不是剪切/粘贴加上一堆find / replace来获得正确的格式的数据。



YAML格式需要花费更多的时间来熟悉,但是使用PyYAML比使用ConfigParser更简单,其优点是你有更多的选择,如何使用YAML表示你的数据。



任何一个听起来像它会满足您当前的需求,ConfigParser将更容易开始,而YAML给你更多的灵活性,如果你的需求扩展。



最好的运气!


I have a python script that analyzes a set of error messages and checks for each message if it matches a certain pattern (regular expression) in order to group these messages. For example "file x does not exist" and "file y does not exist" would match "file .* does not exist" and be accounted as two occurrences of "file not found" category.

As the number of patterns and categories is growing, I'd like to put these couples "regular expression/display string" in a configuration file, basically a dictionary serialization of some sort.

I would like this file to be editable by hand, so I'm discarding any form of binary serialization, and also I'd rather not resort to xml serialization to avoid problems with characters to escape (& <> and so on...).

Do you have any idea of what could be a good way of accomplishing this?

Update: thanks to Daren Thomas and Federico Ramponi, but I cannot have an external python file with possibly arbitrary code.

解决方案

You have two decent options:

  1. Python standard config file format using ConfigParser
  2. YAML using a library like PyYAML

The standard Python configuration files look like INI files with [sections] and key : value or key = value pairs. The advantages to this format are:

  • No third-party libraries necessary
  • Simple, familiar file format.

YAML is different in that it is designed to be a human friendly data serialization format rather than specifically designed for configuration. It is very readable and gives you a couple different ways to represent the same data. For your problem, you could create a YAML file that looks like this:

file .* does not exist : file not found
user .* not found : authorization error

Or like this:

{ file .* does not exist: file not found,
  user .* not found: authorization error }

Using PyYAML couldn't be simpler:

import yaml

errors = yaml.load(open('my.yaml'))

At this point errors is a Python dictionary with the expected format. YAML is capable of representing more than dictionaries: if you prefer a list of pairs, use this format:

-
  - file .* does not exist 
  - file not found
-
  - user .* not found
  - authorization error

Or

[ [file .* does not exist, file not found],
  [user .* not found, authorization error]]

Which will produce a list of lists when yaml.load is called.

One advantage of YAML is that you could use it to export your existing, hard-coded data out to a file to create the initial version, rather than cut/paste plus a bunch of find/replace to get the data into the right format.

The YAML format will take a little more time to get familiar with, but using PyYAML is even simpler than using ConfigParser with the advantage is that you have more options regarding how your data is represented using YAML.

Either one sounds like it will fit your current needs, ConfigParser will be easier to start with while YAML gives you more flexibilty in the future, if your needs expand.

Best of luck!

这篇关于配置文件中的键值对的列表在python中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆