python 老男孩员工信息脚本
1、在文件中读取用户信息登录
2、管理登录用户,增删改查
3、管理用户信息,增删改查
4、用到的知识如下
5、Json repr file
6、Python 有办法将任意值转为字符串:将它传入repr() 或str() 函数。
函数str() 用于将值转化为适于人阅读的形式,而repr() 转化为供解释器读取的形式
7、Json
对简单数据类型的encoding 和decoding
import json
str = ["{'admin': 'jine', 'test': 123456}"]
encodedjson =json.dumps(str)
print repr(obj)
print encodedjson
json.dumps()方法返回了一个str对象encodedjson,我们接下来在对encodedjson进行decode,得到原始数据,需要使用的json.loads()函数:
decodejson =json.loads(encodedjson)
print type(decodejson)
print decodejson[4]['key1']
print decodejson
8、
9、脚本如下
10、# -*- coding: utf-8 -*-
11、#!c:\Python27\Python.exe
12、import json
13、import os
14、#print '\033[1;31;40m'
15、#print '\033[0m'
16、def info(name):
17、 fp = file("bbb.txt","r")
18、 line = fp.readlines()
19、 info = (eval(line[0]))
20、if name in info.keys():
21、return info[name]
22、 fp.close()
23、def lock(name):
24、 fp = file("lock.txt","a+")
25、 str = '%s\n' % name
26、 fp.write(str)
27、 fp.close()
28、def readlock():
29、 fp = file("lock.txt","r")
30、 str = fp.readlines()
31、return repr(str)
32、 fp.close()
33、def admininfo():
34、 fp = file("bbb.txt","r")
35、 line = fp.readlines()
36、 info = (eval(line[0]))
37、return info
38、 fp.close()
39、def adminadd(aname,apwd):
40、 info = admininfo()
41、 fp = open("bbb.txt","w")
42、 info[aname]=apwd
43、 info = repr(info)
44、 fp.write(info)
45、 fp.close()
46、print"add or modify %s success" % aname
47、def admindel(aname):
48、 info = admininfo()
49、 fp = open("bbb.txt","w")
50、del info[aname]
51、 info = repr(info)
52、 fp.write(info)
53、 fp.close()
54、print"del %s success" % aname
55、def admin():
56、while True:
57、print"please select "
58、print"\t\https://www.360docs.net/doc/1015879582.html,erlist"
59、print"\t\t2.adduser"
60、print"\t\t3.miduser"
61、print"\t\t4.deluser"
62、print"\t\t5.exit"
63、 ten=None
64、try:
65、 ten=int(raw_input("entry your selected:"))
66、except:
67、print"please entry 1 or 2 or 3 or 4 or 5"
68、if type(ten)==int:
69、if ten == 1:
70、 adminlist = admininfo()
71、 i = 1
72、for key in adminlist:
73、print"\t\t%s " % i,key,adminlist[key]
74、 i += 1
75、elif ten == 2:
76、while True:
77、####
78、while True:
79、 name = raw_input("Entry new username:")
80、if len(name) == 0:
81、print"please entry new username!"
82、continue
83、else:
84、break
85、while True:
86、 pwd = raw_input("Entry new password:")
87、if len(pwd) == 0:
88、print"please entry new password!"
89、continue
90、else:
91、break
92、if adminadd(name,pwd):
93、print"add user success"
94、break
95、#####
96、elif ten == 3:
97、while True:
98、####
99、while True:
100、 name = raw_input("Entry old username:") 101、if len(name) == 0:
102、print"please entry old username!" 103、continue
104、else:
105、break
106、while True:
107、 pwd = raw_input("Entry new password:") 108、if len(pwd) == 0:
109、print"please entry new password!" 110、continue
111、else:
112、break
113、 adminadd(name)
114、break
115、#####
116、elif ten == 4:
117、while True:
118、####
119、while True:
120、 name = raw_input("Entry del username:") 121、if len(name) == 0:
122、print"please entry del username!" 123、continue
124、else:
125、break
126、 admindel(name)
127、break
128、#####
129、elif ten == 5:
130、break
131、else:
132、print"please entry 1 or 2 or 3 or 4 or 5" 133、continue
134、def emp():
135、while True:
136、print"please select "
137、print"\t\https://www.360docs.net/doc/1015879582.html,erlist"
138、print"\t\t2.adduser"
139、print"\t\t3.miduser"
140、print"\t\t4.deluser"
141、print"\t\t5.exit"
142、 ten=None
143、try:
144、 ten=int(raw_input("entry your selected:"))
145、except:
146、print"please entry 1 or 2 or 3 or 4 or 5"
147、if type(ten)==int:
148、if ten == 1:
149、print"userlist"
150、elif ten == 2:
151、print"adduser"
152、elif ten == 3:
153、print"miduser"
154、elif ten == 4:
155、print"deluser"
156、elif ten == 5:
157、break
158、else:
159、print"please entry 1 or 2 or 3 or 4 or 5" 160、continue
161、def userlist(name):
162、while True:
163、print"welcome %s,please select" % name
164、print"\t\t1.mangeadmin"
165、print"\t\t2.mangeinfo"
166、print"\t\t3.exit"
167、 ten=None
168、try:
169、 ten=int(raw_input("entry your selected:"))
170、except:
171、print"please entry 1 or 2 or 3"
172、if type(ten)==int:
173、if ten == 1:
174、 admin()
175、elif ten == 2:
176、 emp()
177、elif ten == 3:
178、break
179、else:
180、print"please entry 1 or 2 or 3"
181、continue
182、
183、
184、
185、
186、i = 0
187、while i <3:
188、while True:
189、 name = raw_input("Entry your username:")
190、if len(name) == 0:
191、print"please entry your username!"
192、continue
193、else:
194、break
195、while True:
196、 pwd = raw_input("Entry your password:")
197、if len(pwd) == 0:
198、print"please entry your password!"
199、continue
200、else:
201、break
202、 serchlock = readlock()
203、
204、
205、if name in serchlock:
206、print"username %s is locked,please try other username:"% name
207、continue
208、else:
209、if pwd == info(name):
210、 userlist(name)
211、break
212、else:
213、print"your username or password error,please try again!" 214、 i += 1
215、if i == 3:
216、 lock(name)
217、print"chao guo san ci ,%s locked!" % name
218、continue
219、
220、
221、
222、
223、