How to read multiline .properties file in python

2019-06-24 01:31发布

问题:

I'm trying to read a java multiline i18n properties file. Having lines like:

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

Using this code:

import configobj
properties = configobj.ConfigObj(propertyFileName)

But with multilines values it fails.

Any suggestions?

回答1:

According to the ConfigObj documentation, configobj requires you to surround multiline values in triple quotes:

Values that contain line breaks (multi-line values) can be surrounded by triple quotes. These can also be used if a value contains both types of quotes. List members cannot be surrounded by triple quotes:

If modifying the properties file is out of the question, I suggest using configparser:

In config parsers, values can span multiple lines as long as they are indented more than the key that holds them. By default parsers also let empty lines to be parts of values.

Here's a quick proof of concept:

#!/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'))

Output:

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



回答2:

Thanks for the answers, this is what I finally did:

  • Add the section to the fist line of the properties file
  • Remove empty lines
  • Parse with configparser
  • Remove first line (section added in first step)

This is a extract of the code:

#!/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)

I still have troubles with those multilines that doesn't start with an empty space. I just fixed them manually, but a sed could do the trick.



回答3:

Format your properties file like this:

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


回答4:

Give a try to ConfigParser



回答5:

I don't understand anything in the Java broth, but a regex would help you, I hope:

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)

result

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