问题
I have a file with the format
VarName=Value
.
.
I want to read it into a hash such that H("VarName")
will return the value.
What would be a quick way? (read a set of strings, split all of them where the equality sign is, and then put it into a hash?
I am working with python.
回答1:
The oneliner answer:
H = dict(line.strip().split('=') for line in open('filename.txt'))
(optionally use .split()
with maxsplit=1
if the values could also contain the "=" character)
回答2:
Maybe ConfigParser can help you.
回答3:
Taking @Steven's answer doesn't account comments and newlines in the properties file, this one does:
H = dict(line.strip().split('=') for line in open('file.properties') if not line.startswith('#') and not line.startswith('\n'))
回答4:
d = {}
with open('filename') as f:
for line in f:
key, value = line.split('=')
d[key] = value
Edit: As suggested by foret, you could change it to
for line in f:
tokens = line.split('=')
d[tokens[0]] = '='.join(tokens[1:])
which would handle the case where equals signs were allowed in the value, but would still fail if the name could have equals signs as well -- for that you would need a true parser.
回答5:
Or ConfigObj
回答6:
this may be a stupid answer but who know maybe it can help you :)
change the extension of your file to .py, and do necessary change like this:
file.py
VarName="Value" # if it's a string
VarName_2=1
# and you can also assign a dict a list to a var, how cool is that ?
and put it in your package tree or in sys.path, and now you can call it like this in the script when you want to use it:
>>> import file
>>> file.VarName
'Value'
why i'm writing this answer it's because ,what the hell is this file ? i never see a conf file like this , no section no nothing ? why you want to create a config file like this ? it look like a bad config file that should look like the Django settings, and i prefer using a django setting-like config file when ever i can.
Now you can put your -1 in the left :)
回答7:
The csv module will let you do this easily enough:
import csv
H = dict([(row[0], row[1]) for row in csv.reader(open('the_file', 'r'), delimiter='=' )])
回答8:
For python2 there is a jproperties https://pypi.python.org/pypi/jproperties/1.0.1
For python2/3 there is javaproperties http://javaproperties.readthedocs.io/en/v0.1.0/
as simple as:
import os, javaproperties
with open(file, 'rb') as f:
properties_dict = javaproperties.load(f)
回答9:
OK nobody else in the answers has mentioned it, so I guess I'm going to. If you're writing Python, and have control over your interpreter, maybe you can force the use of the Jython interpreter.
Jython is a Python interpreter implemented entirely in Java. You have all the Python standard libraries at your fingertips, with the additional advantage of all your Java SE libraries available.
I haven't actually executed any of the following (think of it more like psudeo-code without exception handling), but you can mix and match Python and Java libraries, and your code might end up looking something like:
from java.util import Properties
from java.io import File, FileInputStream
import os
javasPropertyObject = Properties()
pathToPropFile = os.path.join('path', 'to', 'property', 'file.properties')
if os.path.isfile(pathToPropFile):
#this is java.io.File, not Python's file descriptor
propFile = File(pathToPropFile )
javasFileInputStreamObject = FileInputStream(propFile)
javasPropertyObject.load(javasFileInputStreamObject)
#now we can pull out Java properties as defined by the .property file grammar
myProp = javasPropertyObject.getProperty('myPropName')
where a file like this will be valid, that wouldn't in the simple split on '='
solutions:
myPropName1:value
myPropName2=value
myPropName3=\
value
#this is a = comment
myPropName4:my \
value
myPropNameWithUnicode=\u0009
The downside, is that you lose your ability to be portable among varying Python interpreters and now you're locked into Jython. You would be locked into a library if you attempt that approach as well. The reason why I like Jython is that your added flexibility with having all of the Java SE libraries available.
回答10:
If you need to read all values from a section in properties file in a simple manner:
Your config.properties file layout :
[SECTION_NAME]
key1 = value1
key2 = value2
You code:
import configparser
config = configparser.RawConfigParser()
config.read('path_to_config.properties file')
details_dict = dict(config.items('SECTION_NAME'))
This will give you a dictionary where keys are same as in config file and their corresponding values.
details_dict becomes
{'key1':'value1', 'key2':'value2'}
Now to get key1's value :
value_1 = details_dict['key1']
Putting it all in a method which reads that section from config file only once(the first time the method is called during a program run).
def get_config_dict():
if not hasattr(get_config_dict, 'config_dict'):
get_config_dict.config_dict = dict(config.items('SECTION_NAME'))
return get_config_dict.config_dict
Now call the above function and get the required key's value :
config_details = get_config_dict()
key_1_value = config_details['key1']
来源:https://stackoverflow.com/questions/3997777/what-would-be-a-quick-way-to-read-a-property-file-in-python