Reputation: 160
I am new learning python and I have to deal with the properties file in that file the value will be present in key value pair and some how I think managed to readFile but not able to store or print the value based on a key.
I have tried with the jproperties libraries installed through pip.I have read the values in the object but unable to fetch the records from it. Have gone through https://pypi.org/project/jproperties/#parsing-a-property-file website for reference
from jproperties import Properties
class PropertiesReader:
p = Properties()
with open("foobar.properties", "rt") as f:
p.load(f, "utf-8")
s = p.__getitem__("name","value")
z = p.__getattribute__("email","mail")
print(s)
print(z)
and the properties file
foobar.properties
name = Harsh
email = abc.xyz
and the output is
Traceback (most recent call last):
File "/home/harshk/PycharmProjects/demoPythonPOC/scratch.py", line 4, in <module>
class PropertiesReader:
File "/home/harshk/PycharmProjects/demoPythonPOC/scratch.py", line 7, in PropertiesReader
p.load(f, "utf-8")
File "/usr/local/lib/python3.7/site-packages/jproperties.py", line 804, in load
self._parse()
File "/usr/local/lib/python3.7/site-packages/jproperties.py", line 731, in _parse
while self._parse_logical_line():
File "/usr/local/lib/python3.7/site-packages/jproperties.py", line 686, in _parse_logical_line
self._skip_whitespace()
File "/usr/local/lib/python3.7/site-packages/jproperties.py", line 438, in _skip_whitespace
c = self._peek()
File "/usr/local/lib/python3.7/site-packages/jproperties.py", line 378, in _peek
c = self._source_file.read(1)
File "/usr/local/lib/python3.7/codecs.py", line 500, in read
data = self.bytebuffer + newdata
TypeError: can't concat str to bytes
Process finished with exit code 1
I want to print like
Harsh
abc.xyz
Upvotes: 0
Views: 4087
Reputation: 1895
Test below code : https://repl.it/repls/EmptyRowdyCategories
from jproperties import Properties
p = Properties()
with open("foobar.properties", "rb") as f:
p.load(f, "utf-8")
print(p["name"].data)
print(p["email"].data)
Upvotes: 3
Reputation: 369
You're opening the file as if it's a text file:
with open("foobar.properties", "rt") as f:
p.load(f, "utf-8")
But the jproperties docs shows that you need to open the file in binary mode:
with open("foobar.properties", "rb") as f:
p.load(f, "utf-8")
Upvotes: 1