python函數類比mysql增刪改查功能

來源:互聯網
上載者:User

標籤:info   ati   str   taf   update   格式   write   產生   類比   

import oslist1 = [‘staff_id‘, ‘name‘, ‘age‘, ‘phone‘, ‘dept‘, ‘enroll_date‘]def staff_info():    #擷取員工資訊產生器函數    with open(‘staff_table.txt‘, ‘r‘, encoding=‘utf-8‘) as f:            for line in f :             x =line.split(‘,‘)             staff_dic = {k: v for (k, v) in zip(list1,x)}             yield staff_dicdef select_func(select_content,start,middle,end):    #尋找函數    if middle == ‘=‘:        middle = ‘==‘    if select_content == ‘*‘:        content = list1    else:        content = select_content.split(‘,‘)    total_check = 0    for i in staff_info():        list = []        for j in content:            list.append(i[j])        #尋找需要列出的項        if middle == ‘like‘:            l=len(end)            if eval(i[start][:l] + ‘==‘ +end ):                print(list)                total_check+=1        else:            s = i[start]            e = end            if eval("s"+middle+"e"):                print(list)                total_check+=1    print(‘相關查詢共計%s‘%total_check)def insert_func(list):    #添加功能函數,添加的內容以列表寫入    with open(‘staff_table.txt‘,‘r+‘,encoding=‘utf-8‘) as f:        #判斷手機號是否重複,若重複報錯退出        for line in f:            if list[2] in line:                print(‘err information‘)                return        #不重複的新增內容        f.seek(0)        info=f.readlines()        staff_id=info[-1].split(‘,‘)        staff_id=int(staff_id[0])+1        f.seek(0,2)        s=‘‘        for i in list:            if type(i)==int:                i=str(i)            s=s+‘,‘+i        f.write(str(staff_id)+s+‘\n‘)def delete_func(staff_id):    #刪除資訊函數    with open(‘staff_table.txt‘,‘r+‘,encoding=‘utf_8‘) as f,          open(‘new.txt‘,‘w‘,encoding=‘utf-8‘) as f1:        for line in f:            if staff_id == line[0]:                line=‘‘            f1.write(line)    os.remove(‘staff_table.txt‘)    os.rename(‘new.txt‘,‘staff_table.txt‘)def modify_func(oldcontent,newcontent):    #修改函數    with open(‘staff_table.txt‘,‘r+‘,encoding=‘utf_8‘) as f,          open(‘new.txt‘,‘w‘,encoding=‘utf-8‘) as f1:        for line in f:            if oldcontent in line:                line=line.replace(oldcontent,newcontent)            f1.write(line)    os.remove(‘staff_table.txt‘)    os.rename(‘new.txt‘,‘staff_table.txt‘)def run_func():   #運行函數   while 1:       cmd=input(‘>>>: ‘)       if cmd == ‘quit‘:           break       else:           cmd=cmd.split()           if cmd[0] == ‘select‘:               end=cmd[-1]               start=cmd[-3]               middle=cmd[-2]               select_content=cmd[1]               select_func(select_content,start,middle,end)           elif cmd[0] == ‘insert‘:               list=cmd[1].split(‘,‘)               insert_func(list)           elif cmd[0].lower() == ‘update‘:               oldcontent=cmd[-1]               newcontent=cmd[5]               modify_func(oldcontent,newcontent)           elif cmd[0] == ‘delete‘:               staff_id=cmd[1]               delete_func(staff_id)           else:               print(‘err input‘)run_func()

staff_table.txt 裡的內容:

1,Alex Li,22,13651054608,IT,2013-04-01
2,Jack Wang,30,13304320533,HR,2015-05-03
3,Rain Liu,25,1383235322,Sales,2016-04-22
4,Mack Cao,40,1356145343,HR,2009-03-01

因能力有限,命令格式請遵循下列說明
  • 查詢命令格式:select age,name from satff_table where dept == IT/age > 20/其他條件
  • 修改命令格式:update staff_table set dept = market where where dept = IT
  • 增加命令格式:insert mona, 15, 13693041938, IT, 2016-09-04
  • 刪除命令格式:delete 1

python函數類比mysql增刪改查功能

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.