/
config.py
56 lines (47 loc) · 1.32 KB
/
config.py
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import configparser
import json
account_data={}
class myconf(configparser.ConfigParser):
'''
由于ConfigParser会自动将配置文件的大写字母转换为小写字母,所以需要重写下optionxform方法
'''
def __init__(self,defaults=None):
configparser.ConfigParser.__init__(self,defaults=None)
def optionxform(self, optionstr):
return optionstr
conf = myconf() #声明一个全局的configparse
def getConfig(filename,sectionName):
'''
获得配置文件内容
:param filename: 配置文件名
:param sectionName: 字段名
:return: 账号信息键值对
'''
conf.read(filename)
kvs = conf.items(sectionName)
return kvs
def modifyConfig(filename,sectionName):
'''
:param filename:
:param sectionName:
:return:
'''
conf.add_section(sectionName)
for UID,PWD in account_data.items():
print(UID,PWD)
conf.set(sectionName, UID, PWD)
with open(filename,'w') as f:
conf.write(f)
def get_jsonData(filename):
'''
获得账号信息
:param filename:
:return:
'''
global account_data
with open('d.json','r') as f:
account_data = json.load(f)
return account_data
if __name__ == "__main__":
# modifyConfig("dada.conf", "config")
account = getConfig("dada.conf", "account")