国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

python對配置文件.ini進行增刪改查操作的方法示例

2020-02-16 01:58:45
字體:
來源:轉載
供稿:網友

前言

本文主要給大家介紹的是關于python對配置文件.ini增刪改查操作的相關內容,分享出來供大家參考學習,下面話不多說了,來一起看看詳細的介紹:

一、先導入configobj庫文件

可以用pip直接安裝

#!/usr/bin/python# -*- coding: utf-8 -*-import jsonfrom configobj import ConfigObj

二、增添section

這里是前后端分離的例子,從前端接收json數據,然后寫入配置文件中

def add(self, false=None): self.log.debug("list") try:  conf_ini = CONFIG_INI_PATH+"users.ini.bak"  config = ConfigObj(conf_ini, encoding='UTF8')  req = self.input["input"]  data = req["data"]  userName = data["userName"]  disc = data["disc"]  ip = data["ip"]  expMonth = int(float(data["expDate"]) * 12)  for user in config.items():   if userName == user[0]:    self.out = '{"status": 1,"msg":"用戶名已存在!"}'    return false   else:    pass  config[userName] = {}  config[userName]['user'] = userName  config[userName]['disc'] = disc  config[userName]['ip'] = ip  config[userName]['validity_date'] = data["expDate"]  config[userName]['cert_expired'] = get_today_month(expMonth)  config[userName]['enable'] = 0  config[userName]['path'] = USER_KEY_PATH + userName  config.write()  self.out = '{"status": 0,"msg":"操作成功!"}' except Exception, e:  self.out = '{"status":1, "msg":"'+str(e)+'"}'

三、修改section

def modify(self): self.log.debug("modify") try:  conf_ini = CONFIG_INI_PATH + "users.ini.bak"  config = ConfigObj(conf_ini, encoding='UTF8')  req = self.input["input"]  data = req["data"]  userName = data["userName"]  disc = data["disc"]  ip = data["ip"]  config[userName]['disc'] = disc  config[userName]['ip'] = ip  config.write()  self.out = '{"status": 0,"msg":"操作成功!"}' except Exception, e:  self.out = '{"status":1, "msg":"'+str(e)+'"}'

四、刪除section

通過section名找到相應section進行del操作

def delete(self, false=None): self.log.debug("delete") try:  conf_ini = CONFIG_INI_PATH + "users.ini.bak"  config = ConfigObj(conf_ini, encoding='UTF8')  req = self.input["input"]  data = req["data"]  userName = data["userName"]  for user in config.items():   if userName == user[0]:    del config[userName]    config.write()    self.out = '{"status": 0,"msg":"操作成功!"}'    return false   else:    pass  self.out = '{"status": 1,"msg":"用戶不存在!"}' except Exception, e:  self.out = '{"status":1, "msg":"config err!"}'

五、查詢section

這里借用python字典將配置文件里的內容整體輸出,代碼里還有查詢和分頁的功能

def list(self): self.log.debug("list") try:  req = self.input["input"]  data = req["data"]  pageSize = req["pageSize"]  pageIndex = req["pageIndex"]  userName = data["userName"]  conf_ini = CONFIG_INI_PATH + "users.ini.bak"  config = ConfigObj(conf_ini, encoding='UTF8')  users = []  n = 0  if userName == '':   for user in config.items():    n = n + 1    if pageSize * pageIndex + 1 <= n <= pageSize * (pageIndex + 1):     users.append(user[1])    else:     pass  else:   for user in config.items():    if userName == user[0]:     n = n + 1     if pageSize * pageIndex + 1 <= n <= pageSize * (pageIndex + 1):      users.append(user[1])     else:      pass    else:     pass  utext = json.dumps(users)  self.out = '{"status": 0,"total":'+str(n)+',"data":' + utext + '}' except Exception, e:  self.out = '{"status":1, "msg":"' + str(e) + '"}' self.log.debug("list in.")            
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 白城市| 赣州市| 田东县| 太仓市| 无为县| 莒南县| 马龙县| 安义县| 肃宁县| 恩平市| 高碑店市| 灵山县| 株洲市| 历史| 高邮市| 什邡市| 江津市| 屯门区| 濉溪县| 婺源县| 临颍县| 丽水市| 抚松县| 密山市| 博客| 双牌县| 汶上县| 无极县| 青神县| 东城区| 夹江县| 曲麻莱县| 黔江区| 太保市| 迁西县| 威宁| 五指山市| 临漳县| 瑞昌市| 临邑县| 珲春市|