员工信息表,计算器,简单爬虫

员工信息表:

#!/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')

员工信息表

  计算器

#!/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)

计算器

  简单爬虫

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

  

posted on 2019-02-23 23:38  元神出窍  阅读(237)  评论(0编辑  收藏  举报

导航