【发布时间】:2020-07-21 06:45:50
【问题描述】:
这是我的属性文件的内容,文件名为 conf.properties
#A_Value='true'
#cache_locators='Server_name:1212'
#ssl_trustore='qa_client_truststore'
#ssl_password='ENC(kYE9WAv/HYjdw='=')'
我正在尝试将此属性文件转换为 json
将 conf.properties 转换为 json 的代码
import json
import sys
prop_file = "conf.properties"
import pathlib
file = pathlib.Path(prop_file)
if file.exists ():
split_properties=[line.split("=") for line in open(prop_file)]
properties={key: value for key,value in split_properties }
print(json.dumps(properties))
else:
print (prop_file+" not found")
由于config.properties 的最后一行有多个"=" 符号,我收到以下错误-
properties={key: value for key,value in split_properties }
ValueError: too many values to unpack (expected 2)
我需要删除config.properties最后一行出现的多次“=”符号
来自
#ssl_password='ENC(kYE9WAv/HYjdw='=')'
到
#ssl_password='ENC(kYE9WAv/HYjdw)'
【问题讨论】:
标签: python python-3.x properties