我是新学习python的人,我必须处理该文件中的属性文件,该值将存在于键值对中,以及一些我认为设法读取文件但无法基于键存储或打印该值的方式
我尝试通过pip安装的jproperties库。我已经读取了对象中的值,但是无法从中获取记录。 已访问https://pypi.org/project/jproperties/#parsing-property-file网站以供参考
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)
和属性文件
foobar.properties
name = Harsh
email = abc.xyz
,输出为
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
我想要打印
Harsh
abc.xyz
答案 0 :(得分:1)
您正在打开文件,就好像它是文本文件一样:
(function resize(){
if (document.body.clientWidth<=640) {
$('.quote-heading').readmore({
speed: 75,
lessLink: '<a href="#">Less</a>',
moreLink:'<a href="#">Read more...</a>',
collapsedHeight: 100,
blockProcessed:function(element,collapsable){
console.log(element);
}
});
}
if (document.body.clientWidth>640){
$('.quote-heading').readmore('destroy');
}
})();
但是jproperties docs显示您需要以二进制模式打开文件:
with open("foobar.properties", "rt") as f:
p.load(f, "utf-8")
答案 1 :(得分:1)
测试以下代码: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)