跳转到主要内容

Java .properties文件解析器

项目描述

Java .properties文件 解析器。

支持Python 2.6, 2.7, 和 3.3+

安装

使用pip从PyPI安装jprops

pip install jprops

使用方法

读取属性

使用 jprops.load_properties 读取属性文件并返回一个普通的Python dict

import jprops
with open('mine.properties') as fp:
  properties = jprops.load_properties(fp)

您可以为加载属性到不同的数据结构提供一个自定义的“映射”。例如,如果您想保持属性与它们最初出现时的相同顺序,您可以使用“有序字典”,例如 collections.OrderedDict。 “映射”可以是任何接受可迭代(key, value)对的类型或函数

import collections
with open('mine.properties') as fp:
  properties = jprops.load_properties(fp, collections.OrderedDict)

load_properties 只是 iter_properties 的包装,如果您想懒加载属性而不将它们全部加载到数据结构中,您可以直接使用它

with open('mine.properties') as fp:
  for key, value in jprops.iter_properties(fp):
    if key.startswith('foo'):
      print key, value

写入属性

使用 jprops.store_propertiesdict、类似于字典的对象或任何可迭代(key, value)对写入文件

x = {'y': '1', 'z': '2'}
with open('out.properties', 'w') as fp:
  jprops.store_properties(fp, x)

默认情况下,jprops遵循Java约定,在文件开头写入时间戳注释,如下所示:

#Thu Oct 06 19:08:50 EDT 2011
y=1
z=2

您可以通过传递 timestamp=False 来抑制写入时间戳注释。

您可以为时间戳之前出现的自定义标题注释提供。多行注释将被适当地处理,注释将跨行继续

jprops.store_properties(fp, {'x': '1'}, comment='Hello\nworld!')
#Hello
#world!
#Thu Oct 06 19:17:21 EDT 2011
x=1

您还可以使用 write_commentwrite_property 来对写入属性文件进行更细致的控制

with open('out.properties', 'w') as fp:
  jprops.write_comment(fp, 'the hostname:')
  jprops.write_property(fp, 'host', 'localhost')
  jprops.write_comment(fp, 'the port number:')
  jprops.write_property(fp, 'port', '443')
#the hostname:
host=localhost
#the port number:
port=443

注释

默认情况下,输入中的注释将被忽略,但可以通过将comments=True传递给iter_properties来包含它们。注释将使用jprops.COMMENT作为哨兵值包含在键的替代位置。

with open('in.properties') as fp:
  props = list(jprops.iter_properties(fp, comments=True))
for k, v in props:
  if k is jprops.COMMENT:
    print 'comment:', v

jprops不包含用于保留注释的特殊数据结构,但在将属性写回之前可以操作它们。例如,这是在写入输出时更改属性的一个简单模式。

updates = {'one': '1', 'two': '2', 'to_remove': None}

with open('out.properties', 'w') as fp:
  for key, value in props:
    # updates.pop will return and remove the value for the key, or return
    # the original `value` if it doesn't exist
    value = updates.pop(key, value)
    # skip keys set to `None` in `updates`
    if value is not None:
      # write_property handles jprops.COMMENT as the key so you don't have to
      # check whether to use write_comment
      jprops.write_property(fp, key, value)
  # since the existing keys have already been popped, use store_properties
  # to write the remaining updates
  jprops.store_properties(fp, updates, timestamp=False)

文件编码和Unicode

以二进制模式打开的文件,例如open(filename, 'rb')open(filename, 'wb')将使用latin-1编码,并以\uffff格式转义Unicode字符,以与Java Properties字节流编码兼容。

从版本2.0开始,也支持以其他文本编码打开的文件。

with io.open('sample.properties', encoding='utf-8') as fp:
  props = jprops.load_properties(fp)

这适用于内置的open函数、codecs.openio.open。其他扩展了io.TextIOBase或具有非空encoding属性的文件-like对象将以Unicode文本值读取或写入,否则将视为二进制,并以latin-1编码的字节读取或写入。

作者

Matt Good (matt@matt-good.net)

更改

2.0.2 (2017-04-21)

  • 在发布前在setup.py中增加版本号

2.0.1 (未发布)

  • 修复值中的过度转义

2.0 (2017-04-08)

  • 支持使用文本编码打开的文件

  • jprops.COMMENT的漂亮repr

1.0 (2013-06-12)

  • Python 3.3支持

  • 尝试写入非字符串值时提供更详细的信息性错误

0.2 (2012-05-02)

  • 处理Windows或Mac行结束符

0.1 (2011-10-07)

初始发布。

项目详情


下载文件

下载适合您平台的文件。如果您不确定选择哪个,请了解有关安装包的更多信息。

源分发

jprops-2.0.2.tar.gz (6.3 kB 查看散列)

上传时间

构建分发

jprops-2.0.2-py2.py3-none-any.whl (9.1 kB 查看散列)

上传时间 Python 2 Python 3

支持者