Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what would be a quick way to read a property file in python?

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.

like image 546
jury Avatar asked Oct 22 '10 14:10

jury


People also ask

What is a properties file in Python?

We can use jproperties module to read properties file in Python. A properties file contains key-value pairs in each line. The equals (=) works as the delimiter between the key and value. A line that starts with # is treated as a comment.

How read JSON properties file in Python?

Reading From JSON Python has a built-in package called json, which can be used to work with JSON data. It's done by using the JSON module, which provides us with a lot of methods which among loads() and load() methods are gonna help us to read the JSON file.

How will we access the data from the properties file?

The Properties is a subclass of Hashtable class and it represents a persistent set of properties. The Properties can be saved to a stream or loaded from a stream. Each key and its corresponding value in the property list is a string.


8 Answers

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)

like image 115
Steven Avatar answered Oct 04 '22 07:10

Steven


Maybe ConfigParser can help you.

like image 27
rubik Avatar answered Oct 04 '22 09:10

rubik


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.

like image 39
user470379 Avatar answered Oct 04 '22 08:10

user470379


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'))  
like image 42
GBF_Gabriel Avatar answered Oct 04 '22 09:10

GBF_Gabriel


Or ConfigObj

like image 41
mmmmmm Avatar answered Oct 04 '22 09:10

mmmmmm


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='=' )])
like image 27
GreenMatt Avatar answered Oct 04 '22 08:10

GreenMatt


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 :)

like image 40
mouad Avatar answered Oct 04 '22 08:10

mouad


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)
like image 26
Igor A Avatar answered Oct 04 '22 08:10

Igor A