員工信息表:html
#!/usr/bin/env python #-*-coding=utf-8 -*- #GKX column_dic = {'id':0,'name':1,'age':2,'iphone':3,'job':4} def get_show_lst(col_condition):#'select name,age' ''' 獲取要展現的列名 :param col_contion: 用戶輸入的select條件 :return: 列名組成的字典 ''' col_info_lst = col_condition.strip().split('select') col_info_lst = [col_info_item for col_info_item in col_info_lst if col_info_item .strip()] if col_info_lst: col_info = col_info_lst[0].strip() if '*' == col_info: return column_dic.keys() elif col_info: ret = col_info.split(',') return [item.strip() for item in ret] # else: # print(col_info) def filter_handler(operate,con): # '>','age > 22' ''' 進行篩選工做 :param operate:用戶要進行的操做是 < > = like :param con:用戶輸入的where條件 :return:被選中的全部行組成的列表,其中每一行都是一個列表 ''' selected_lst = [] col,val = con.split(operate) col = col.strip() val = val.strip() judge = 'int(line_lst[column_dic[col]]) %s int(val)'%operate if operate=='<' or operate =='>' \ else 'line_lst[column_dic[col]] %s val'%operate f = open('info_txt',encoding='utf-8') for line in f: line_lst = line.strip().split(',') if eval(judge): selected_lst.append(line_lst) f.close() return selected_lst def get_selected_line(con): # 'age > 22' ''' 獲取全部要查找的行,並將每一行做爲一個列表項存儲在selected_lst中 :param con: :return: ''' if '>' in con: selected_lst = filter_handler('>',con) elif '<' in con: selected_lst = filter_handler('<', con) elif '=' in con: selected_lst = filter_handler('==', con.replace('=','==')) elif 'like' in con: selected_lst = filter_handler('in',con) return selected_lst def show(selected_lst,show_lst): for selected_item in selected_lst: for col in show_lst: print(selected_item[column_dic[col]],end=' ') print() while True: condition = input('>>>') #接收用戶指令 #解析用戶指令 if 'where' in condition: ret = condition.split('where') #['select name,age','age > 22'] con = ret[1].strip() #'age > 22' # 根據select條件解析用戶須要展現的內容 show_lst = get_show_lst(ret[0]) # 'select name,age' # 根據where條件解析篩選用戶想查找的內容 selected_lst = get_selected_line(con) # 將符合條件的內容按照用戶的需求展現出來 show(selected_lst, show_lst) choice = input('continue?') if choice == 'q': break else: print('selection without where')
計算器python
#!/usr/bin/env python #-*-coding=utf-8 -*- #GKX import re def dealwith(express): ''' 將表達式中 ‘+-’替換爲 - --替換爲+ :param new_express: 計算完原子型表達式後的主表達式 :return: ''' express:express = express.replace('+-','-') express:express = express.replace('--','+') return express def cal_exp_son(exp_son): ''' 計算原子型表達式 兩個數之間的乘除法 :param exp_son: 傳進來的,只有兩個數之間的乘除法 :return: 返回計算後的值 ''' if '/' in exp_son: a,b = exp_son.split('/') # -40/5 return str(float(a)/float(b)) elif '*' in exp_son: a,b = exp_son.split('*') return str(float(a)* float(b)) def cal_express_no_bracket(exp): ''' 計算裏層不帶括號的表達式 :param exp: 最裏層不帶括號的表達式傳進來 :return: ''' exp = exp.strip('()') #先乘除後加減 #-40/5*8+2 9-2*5/3+7/3*99/4*2998+10*568/14 while True: ret = re.search('\d+\.?\d*[*/]-?\d+\.?\d*',exp) if ret: exp_son = ret.group() #子表達式 最簡單的乘除法 ret = cal_exp_son(exp_son) exp = exp.replace(exp_son,ret) #ret爲計算後的值,替換爲 乘除的式子 #111print(exp) exp = dealwith(exp) #111print(exp) else: #當表達式裏沒有了乘除法 ret = re.findall('-?\d+\.?\d*',exp) sum = 0 for i in ret: sum += float(i) return str(sum) #提取括號裏面沒有其餘括號的表達式 def remove_bracket(new_express): while True: ret = re.search('\([^()]+\)',new_express) if ret: express_no_bracket = ret.group() #111print('匹配到內部再也不有括號的值',express_no_bracket) #表達式沒括號 ret = cal_express_no_bracket(express_no_bracket) #print(new_express,express_no_bracket,ret) new_express = new_express.replace(express_no_bracket,ret) #print(new_express) #new_express = dealwith(new_express) #111print(new_express) else: #111print('表達式中沒有括號了',new_express) ret = cal_express_no_bracket(new_express) #111print(ret) return ret #去空格 def calculator(express): #express = input('表達式') if express.count('(') != express.count(')'): print('表達式括號有誤,請檢查並從新輸入') else: new_express = re.sub('[\s]','',express) #express.replace(' ','') #11print(new_express) res = remove_bracket(new_express) print(res) express = '1 - 2 * ( ( (6 0) -3 0 +(-40/5) * (9-2*5/3 + 7 /3*99/4*2998 +10 * 568/14 )) - (-4*3)/ (16-3*2) )' calculator(express)
簡單爬蟲express
import requests import re import json def getPage(url): response=requests.get(url) return response.text def parsePage(s): com=re.compile('<div class="item">.*?<div class="pic">.*?<em .*?>(?P<id>\d+).*?<span class="title">(?P<title>.*?)</span>' '.*?<span class="rating_num" .*?>(?P<rating_num>.*?)</span>.*?<span>(?P<comment_num>.*?)評價</span>',re.S) ret=com.finditer(s) for i in ret: yield { "id":i.group("id"), "title":i.group("title"), "rating_num":i.group("rating_num"), "comment_num":i.group("comment_num"), } def main(num): url='https://movie.douban.com/top250?start=%s&filter='%num response_html=getPage(url) ret=parsePage(response_html) print(ret) f=open("move_info7","a",encoding="utf8") for obj in ret: print(obj) data=json.dumps(obj,ensure_ascii=False) f.write(data+"\n") if __name__ == '__main__': count=0 for i in range(10): main(count) count+=25