【问题标题】:How to read multiline .properties file in python如何在python中读取多行.properties文件
【发布时间】:2011-08-24 11:38:03
【问题描述】:

我正在尝试读取 java 多行 i18n 属性文件。有这样的行:

messages.welcome=Hello\
 World!
messages.bye=bye

使用此代码:

import configobj
properties = configobj.ConfigObj(propertyFileName)

但是对于多行值,它会失败。

有什么建议吗?

【问题讨论】:

    标签: python properties multiline


    【解决方案1】:

    根据ConfigObj documentationconfigobj 要求您将多行值括在三引号中:

    包含换行符的值 (多行值)可以被包围 通过三重引号。这些也可以 如果一个值包含两种类型 引号。列表成员不能 用三引号括起来:

    如果修改属性文件是不可能的,我建议使用configparser

    在配置解析器中,值可以跨越 多行,只要它们是 缩进多于持有的键 他们。默认情况下,解析器也让 空行作为值的一部分。

    这是概念的快速证明:

    #!/usr/bin/env python
    # coding: utf-8
    
    from __future__ import print_function
    
    try:
        import ConfigParser as configparser
    except ImportError:
        import configparser
    
    try:
        import StringIO
    except ImportError:
        import io.StringIO as StringIO
    
    test_ini = """
    [some_section]
    messages.welcome=Hello\
     World
    messages.bye=bye
    """
    config = configparser.ConfigParser()
    config.readfp(StringIO.StringIO(test_ini))
    print(config.items('some_section'))
    

    输出:

    [('messages.welcome', 'Hello World'), ('messages.bye', '再见')]

    【讨论】:

      【解决方案2】:

      感谢您的回答,这就是我最终所做的:

      • 将该部分添加到属性文件的第一行
      • 删除空行
      • 使用 configparser 解析
      • 删除第一行(第一步添加的部分)

      这是代码的摘录:

      #!/usr/bin/python
      ...
      # Add the section
      subprocess.Popen(['/bin/bash','-c','sed -i \'1i [default]\' '+srcDirectory+"/*.properties"], stdout=subprocess.PIPE)
      # Remove empty lines
      subprocess.Popen(['/bin/bash','-c','sed -i \'s/^$/#/g' '+srcDirectory+"/*.properties"], stdout=subprocess.PIPE)
      # Get all i18n files
      files=glob.glob(srcDirectory+"/"+baseFileName+"_*.properties")
      config = ConfigParser.ConfigParser()
      for propFile in files:
      ...
          config.read(propertyFileName)
          value=config.get('default',"someproperty")
      ...
      # Remove section 
      subprocess.Popen(['/bin/bash','-c','sed -i \'1d\' '+srcDirectory+"/*.properties"], stdout=subprocess.PIPE)
      

      我仍然对那些不以空格开头的多行有问题。我只是手动修复它们,但 sed 可以解决问题。

      【讨论】:

        【解决方案3】:

        像这样格式化你的属性文件:

        messages.welcome="""Hello
         World!"""
        messages.bye=bye
        

        【讨论】:

        • 这是一个Java属性文件,我不能修改它们。即使我使用临时文件,格式化也会很困难。
        【解决方案4】:

        试试ConfigParser

        【讨论】:

        • 我已经尝试过了,但它至少需要一个类似于 [default] 的类别。
        【解决方案5】:

        我对 Java 肉汤中的任何内容都一无所知,但我希望正则表达式会对您有所帮助:

        import re
        
        ch = '''messages.welcome=Hello
          World!  
        messages.bye=bye'''
        
        regx = re.compile('^(messages\.[^= \t]+)[ \t]*=[ \t]*(.+?)(?=^messages\.|\Z)',re.MULTILINE|re.DOTALL)
        
        print regx.findall(ch)
        

        结果

        [('messages.welcome', 'Hello\n  World!  \n'), ('messages.bye', 'bye')]
        

        【讨论】:

        • 感谢您的回复。但我的问题是从文本文件中获取赋值,解析没问题
        • @ghm1014 好吧,您不知道读取文件的内容然后对其进行处理以删除换行符和周围的空格以及其他类似的操作吗?
        • 当然可以,但可能会出现太多特殊情况。算上这些是 12 种语言和数千条消息的翻译文件。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-08-18
        • 2022-01-19
        • 2020-07-31
        • 1970-01-01
        • 2018-07-26
        • 1970-01-01
        相关资源
        最近更新 更多