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.
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.
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.
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.
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)
Maybe ConfigParser can help you.
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.
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'))
Or ConfigObj
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='=' )])
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 :)
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With