120prop-python3.7 读写.properties文件
120prop-python3.7 读写.properties文件
[an error occurred while processing the directive] [an error occurred while processing the directive]
#!/usr/bin/python
# -*- coding: UTF-8 -*- import re
import os
import tempfile class Properties: def __init__(self, file_name):
self.file_name = file_name
self.properties = {}
try:
fopen = open(self.file_name, 'r')
for line in fopen:
line = line.strip()
if line.find('=') > 0 and not line.startswith('#'):
strs = line.split('=')
self.properties[strs[0].strip()] = strs[1].strip()
except Exception :
raise e
else:
fopen.close() def has_key(self, key):
return key in self.properties def get(self, key, default_value=''):
if key in self.properties:
return self.properties[key]
return default_value def put(self, key, value):
self.properties[key] = value
replace_property(self.file_name, key + '=.*', key + '=' + value, True) def replace_property(file_name, from_regex, to_str, append_on_not_exists=True):
tmpfile = tempfile.TemporaryFile() if os.path.exists(file_name):
r_open = open(file_name, 'r')
pattern = re.compile(r'' + from_regex)
found = None
for line in r_open:
if pattern.search(line) and not line.strip().startswith('#'):
found = True
line = re.sub(from_regex, to_str, line)
tmpfile.write(line.encode())
if not found and append_on_not_exists:
tmpfile.write(('\n' + to_str).encode())
r_open.close()
tmpfile.seek(0) content = tmpfile.read() if os.path.exists(file_name):
os.remove(file_name) w_open = open(file_name, 'wb')
w_open.write(content)
w_open.close() tmpfile.close()
else:
print ("file %s not found" % file_name) if __name__ == "__main__":
file_path = 'jdbc.properties'
props = Properties(file_path) # 读取文件
props.put('jdbc.url', 'value_a') # 修改/添加key=value
print (props.get('key_a')) # 根据key读取value
print ("props.has_key('key_a')=" + str(props.has_key('key_a'))) # 判断是否包含该key
120prop-python3.7 读写.properties文件的更多相关文章
- Java读写.properties文件实例,解决中文乱码问题
package com.lxk.propertyFileTest; import java.io.*; import java.util.Properties; /** * 读写properties文 ...
- 读写properties文件方法
按key读取properties文件中的value public static String readSystemConfig(String key){ Properties prop = new P ...
- 读写properties文件
1. 读properties文件 Properties props = new Properties(); try { InputStream in = new FileInputStream(&qu ...
- java读写Properties文件
Java 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 ...
- 用java读写properties文件的代码
package com.LY; import java.io.BufferedInputStream; import java.io.FileInputStream; import java.io.F ...
- Java读写资源文件类Properties
Java中读写资源文件最重要的类是Properties 1) 资源文件要求如下: 1.properties文件是一个文本文件 2.properties文件的语法有两种,一种是注释,一种属性配置. 注 ...
- Properties读写资源文件
Java中读写资源文件最重要的类是Properties,功能大致如下: 1. 读写Properties文件 2. 读写XML文件 3. 不仅可以读写上述两类文件,还可以读写其它格式文件如txt等,只要 ...
- Python:解析properties文件
在项目中遇到解析properties的情况,而Python中正好没有解析properties文件的现成模块,于是从网上找到了这个脚本,有一些小地方修改了一下 原博客: Python读写properti ...
- java 读写properties (配置)文件
Properties属性文件在Java应用程序中是经常可以看得见的,也是特别重要的一类文件.它用来配置应用程序的一些信息,不过这些信息一般都是比较少的数据,没有必要使用数据库文件来保存,而使用一般的文 ...
随机推荐
- 在idea下遇到的问题汇总(间接性更新)
在idea下遇到的问题汇总(间接性更新) tomcat下的jsp代码问题: 在idea的环境下,遇到jsp代码.符号失效,首先需要考虑到jar包没有引入,情况如图: 这种情况是因为jar包没有导入进去 ...
- 微信小程序支付到第三方商户账号
使用场景:合作商家使用本公司小程序开店,要求支付金额直接到合作商家的公司微信账户; 使用要求:合作商家需提供微信支付关联,商户号,商户API密钥,API证书(该证书只用作退款功能,不开发退款可以不用) ...
- Mac使用brew安装MongoDB
之前一直使用以下命令安装MongoDB,但是一直安装不上 brew install mongodb 后来看了官网,安装方法如下 brew tap mongodb/brew //这步不知道需不需要 br ...
- 基于JS正则实现模板数据动态渲染
最近业务上需要动态渲染模板数据: 一.业务需求: 1.前端后端定义好模板以及变量名,根据打印机类型转换成对应sdk需要的标签模板,保存数据库 2.订单数据是前端根据支付结果获取的,最终渲染完的数据模板 ...
- H5页面通用头部设置
见到很多人写H5页面都不设置头部,不忍直视,于是整理一篇文章,不定期更新,为了让自己显得专业一点,也为了方便自己复制粘贴 一般来说必须设置项 <!-- 页面编码 --> <meta ...
- 微信h5页面audio标签在ios下不能自动播放
背景介绍:在一个h5页面中,当用户提交表单到后台,后台返回的结果成功的话,开始自动播放背景音乐 出现的问题:在安卓手机上正常,iOS中没有反应 后来网上一番搜索后了解到时因为iOS不允许自动播放音乐, ...
- localStorage,sessionStorage的方法重写
本文是针对于localStorage,sessionStorage对于object,string,number,bollean类型的存取方法 我们知道,在布尔类型的值localStorage保存到本地 ...
- nginx前端服务部署
一.登录服务器 登录跳板机 执行list,列出所有机器 执行dssh 机器序号,如dssh 1,选择机器 二. 创建nginx配置文件 进入nginx配置目录:cd usr/local/nginx/c ...
- unzip详解,Linux系统如何解压缩zip文件?
通常在使用linux时会自带了unzip,但是在最小化安装之后,可能系统里就无法使用此命令了. yum list unzip 查看是否安装 如果没安装过就继续 yum install unzip 安装 ...
- 开发RTSP 直播软件 H264 AAC 编码
上一篇对摄像头预览,拍照做了大概的介绍,现在已经可以拿到视频帧了,在加上 RTSP 实现,就是直播的雏形,当然还要加上一些 WEB 管理和手机平台的支援,就是一整套直播软件. 介绍一些基础概念:RTP ...