1. You need to use time to get the current
Import Time #引用time方法
Now_time = Time.strftime ('%y-%m-%d%h:%m:%s ') #输出当前的时间, the format is fixed, the input result is 2017-06-08 21:21:33
2, when the file is written, want to add a space in the file or newline, the format is as follows:
Where now_time is the defined variable, nathing is the parameter passed in the function, it can be quoted, and they are connected using the + sign
Fl.write (now_time+ ' +nathing+ ' \ n ')
3, when the contents of the file into a dictionary, the password how to determine the user is not consistent with the input
When the dictionary in the file is stored in the following format:
Because each key has two values and is placed in a dictionary, where passwd is the key in the second dictionary, you can continue to use the dictionary to value the way you look, and be careful to quote, because the key in the dictionary is a string, without quotes, it is searched as a variable.
{' Luoluo ': {' money ': "5435, ' passwd ': 234567}, ' admin ': {' money ': ' 12.12 ', ' passwd ': ' 123456 '}}
PWD = = str (user_dic[username][' passwd ')
4, add goods in the case that the price is a decimal, the following is judged as a number and a decimal condition, including positive and negative decimals:---a masterpiece of the Ox and ox God!
23456789101112131415 |
def is_float(s): s = str(s) If s. Count('. ') )= =1:#判断小数点个数 SL = s. Split('. ') )#按照小数点进行分割 Left = sl[0]#小数点前面的 Right = SL[1]#小数点后面的 if left.< Span class= "CRAYON-E" >startswith ( '-' and left count ( '-' ==1 and right. Isdigit () : lleft = left. Split ( '-' ) [< Span class= "CRAYON-CN" >1] #按照-split, then take the number following the minus sign if lleft. IsDigit(): return True Elif left . IsDigit() and right . IsDigit(): #判断是否为正小数 return True return False Print(is_float(' -98.9 ')) |
5, the collection is stored in a dictionary, delete the contents of the file, you need to first move the pointer to the front, and clear before you can, and then write
If P_name in Products_dic: #products_dic是存放产品的字典
Products_dic.pop (P_name) #删除一个商品, now the dictionary is updated with the latest
Fp.seek (0) #将指针移到最前面
Fp.truncate () #清空文件里的所有内容
Fp.write (str (products_dic)) #将更新的最新的字典写到文件里
Print (' Delete product success ')
6. What is handled between functions and functions
If the function has no parameters, the function must have a return value, which returns TRUE or false, which can be judged as a condition when used.
If a function has a number of arguments, the function can be called directly when other functions are used, passing in parameters as needed
Eg: there is a return value if there is no pass argument
def login ():
With open (' Users.txt ') as fr:
Fr.seek (0)
User_str = Fr.read ()
User_dic = eval (user_str)
If username in User_dic:
if pwd = = str (user_dic[username][' passwd ')):
Print ("Congratulations on your successful landing!") ")
Log ("Congratulations on%s landing success!") "%username)
Return True
Else
Print ("The password entered is incorrect!") ")
Return False
Else
Print ("The user name you entered does not exist")
Return False
Eg: executes the function by passing in parameters
Import time
def log (nathing):
Fl=open (' Log.txt ', ' A + ')
Now_time = Time.strftime ('%y-%m-%d%h:%m:%s ')
Fl.write (now_time+ ' +nathing+ ' \ n ')
Log ("Product%s deleted successfully!") "% p_name) #调用时
7, through the dictionary, can be more convenient to call multiple functions
Choice = input (' 1 Add Item, 2 delete item, 3 query item, 4 user management, 5 exit, please enter your choice: '). Strip ()
def hh ():
Print (' Modify Item ... ')
def cc ():
Print (' Add Item ... ')
def query ():
Print (' Search products ... ‘)
def delete ():
Print (' Delete items ... ‘)
menu = {' 1 ': hh, ' 2 ': cc, ' 3 ': Query, ' 4 ': delete}
If choice in menu:
Menu[choice] ()
Else
Print (' Please enter a value between 1-5! ')
8, judge the contents of the file is not empty:
choice = input (' 1 Add Item, 2 query item, 3 exit, please enter your choice: '). Strip ()
fp = open (' Products.txt ', ' A + ')
Fp.seek (0)
Products_str = Fp.read ()
# This is the product information read from the file, is the string
If Len (products_str):
# Here is to determine whether the contents of the file is empty, if not empty, the length is greater than 0, is true br> # Once you're gone this means there's a product
Products_dic = eval (products_str)
# is to turn the read out of the product information into a dictionary
else:
# Go here it means there is no product information
Products_dic = {} # stores all items
9, gets the content entered at execution time, and determines if the file exists
#运行的时候要 python xxx.py hh.txt hehe haha
Import Sys,os
Inputs = SYS.ARGV
# #存的是所有运行时候传进来的参数
# #它就是用来获取在用python命令运行python文件的时候, Parameters passed in
#1, determine whether the user input is sufficient number
If Len (inputs) <4:
Print (' parameter not enough, need at least 3 parameters, E.g:python xx.py xx.txt old_str new_str ... ')
Else
file_name = inputs[1]
OLD_STR = inputs[2]
NEW_STR = inputs[3]
New_file_name = file_name+ '. New '
If Os.path.exists (file_name): #用它来判断文件是否存在
With open (file_name,encoding= ' GBK ') as Fr,open (New_file_name, ' W ') as FW:
For line in FR:
res = Line.replace (old_str,new_str) #替换之后的内容
Fw.write (RES)
Os.remove (file_name)
Os.rename (New_file_name,file_name)
Else
Print (' file does not exist ')
Small knowledge points in Python