1、文件操作練習題
1 —— 全局替換程序:
-
寫一個腳本,允許用戶按以下方式執行時,即可以對指定文件內容進行全局替換
'python your_script.py old_str new_str filename'
-
替換完畢后打印替換了多少處內容
https://www.cnblogs.com/shengyang17/p/8688915.html
2 —— 模擬登陸:
- 用戶輸入帳號密碼進行登陸
- 用戶信息保存在文件內
- 用戶密碼輸入錯誤三次后鎖定用戶,下次再登錄,檢測到是這個用戶也登錄不了
i = 0 file = open("正確用戶信息",'r',encoding='utf-8') file_wrong = open("鎖定信息",'r+',encoding='utf-8') line = eval(file.readline()) line_wrong = eval(file_wrong.readline()) name = input("請輸入用戶名:") passworld = input("請輸入密碼:") if name in line and name not in line_wrong: while i <= 3: if passworld == line[name]: print('登錄成功') break else: i +=1 print('\n您已經第%d次輸錯密碼,輸錯3次賬戶將會被鎖定'%(i)) if i < 3: passworld = input("請重新輸入密碼:") elif i == 3: print("已經輸錯3次,賬戶已被鎖定") line_wrong.append(name) file_wrong.seek(0) file_wrong.write(str(line_wrong)) #file_wrong.tell() break elif name in line_wrong: print("該用戶名已被鎖定") else: print("該用戶名不存在") exit()
2、遞歸練習題
#遞歸練習題 http://www.cnblogs.com/xiao-apple36/p/8607835.html menus = [ { 'text':'北京', 'children':[ {'text':'朝陽','children':[]}, {'text':'昌平','children':[ {'text':'沙河','children':[]}, {'text':'回龍觀','children':[]}, ]} ] }, { 'test':'上海', 'children':[ {'test':'寶山','children':[]}, {'test':'金山','children':[]}, ] } ] #有時面試 深度查詢 #1.打印所有的節點 #1.輸入一個節點名字,沙河,你要遍歷找,找到了,就打印它,並返回true
# 打印所有的節點 def recu_Menu(menu): for sub_menu in menu: menu_text = sub_menu['text'] menu_children = sub_menu['children'] print(menu_text) recu_Menu(menu_children) recu_Menu(menus) # 打印所有的節點,輸入一個節點名字,沙河, 你要遍歷找,找到了,就打印它,並返回true, flag = False def recu_Menu_node(menu, node, layer): global flag for sub_menu in menu: menu_text = sub_menu['text'] menu_children = sub_menu['children'] if node == menu_text: print("找到%s在第%s層" % (node, layer)) flag = True return flag else: recu_Menu_node(menu_children, node, layer + 1) print("menu_text=", menu_text) return flag node_str = input("輸入一個節點名字-> ") print(recu_Menu_node(menus, node_str, 1)) #方法二 menus = [ { 'text': '北京', 'children': [ {'text': '朝陽', 'children': []}, {'text': '昌平', 'children': [ {'text': '沙河', 'children': []}, {'text': '回龍觀', 'children': []}, ]}, ] }, { 'text': '上海', 'children': [ {'text': '寶山', 'children': []}, {'text': '金山', 'children': []}, ] } ] #打印所有的節點 def recu_Menu(menu): for sub_menu in menu: #print(sub_menu) menu_text = sub_menu['text'] menu_children = sub_menu['children'] print(menu_text) recu_Menu(menu_children) recu_Menu(menus) #打印所有的節點,輸入一個節點名字,沙河,你要遍歷找,找到了,就打印它,並返回true def recu_Menu_node(menu, node, layer): # if len(menu)>0: for sub_menu in menu: menu_text = sub_menu['text'] menu_children = sub_menu['children'] print("menu_text=", menu_text) if node == menu_text: print("找到%s在第%s層" % (node, layer)) #返回到外層 node節點| layer層次 return True else: if recu_Menu_node(menu_children, node, layer + 1) == True: #如果里層返回True,繼續向上返回True return True else: recu_Menu_node(menu_children, node, layer + 1) node_str = input("輸入一個節點名字-->>> ") print(recu_Menu_node(menus, node_str, 1))
3、函數練習題
修改個人信息程序,在一個文件里存多個人的個人信息,如以下:
username password age position department alex abc123 24 Engineer IT rain df2@432 25 Teacher Teching ....
1.輸入用戶名密碼,正確后登錄系統 ,打印
1. 修改個人信息
2. 打印個人信息
3. 修改密碼
2.每個選項寫一個方法
3.登錄時輸錯3次退出程序
def print_user_info(account_dic,username): person_info = account_dic[username] msg = ''' Name: %s Age: %s Job: %s Dept: %s Phone: %s ''' % (person_info[2], person_info[3], person_info[4], person_info[5], person_info[6]) print(msg) def save_back_to_file(account_dic): f.seek(0) f.truncate() for line in account_dic: row = ','.join(account_dic[line]) f.write('%s\n' % row) f.flush() def update_user_info(account_dic,username): person_info = account_dic[username] print('old data:', person_info) str = ['Username','Password','Name','Age','Job','Dept','Phone'] for index, i in enumerate(person_info): if index > 1: #用戶名 密碼 不顯示 print('%s. %s: %s' % (index, str[index], i)) choice = input('select column id to change >>>:').strip() if choice.isdigit(): choice = int(choice) if choice > 1 and choice < len(person_info): print('current value:', person_info[choice]) new_val = input('new value:').strip() if new_val: #不為空 person_info[choice] = new_val print('new data:', person_info) save_back_to_file(account_dic) else: print('不能為空!') f = open('account.txt', 'r+', encoding='utf-8') data = f.readlines() print(data) account_dic = {} for i in data: items = i.replace(' ', '').split(',') account_dic[items[0]] = items # 將文件讀出來存到字典形式中 print(account_dic) msg = ''' 1.打印個人信息 2.修改個人信息 ''' count = 0 while count < 3: username = input('Username:').strip() password = input('Password:').strip() if username in account_dic: if password == account_dic[username][1]: print('welcome %s '.center(50,'-') % username) while True: print(msg) user_choice = input('>>>:').strip() if user_choice.isdigit(): user_choice = int(user_choice) if user_choice == 1: print_user_info(account_dic,username) elif user_choice == 2: update_user_info(account_dic,username) elif user_choice == 'q': exit('bye bye') else: print('Wrong Username or password') else: print('Username dose not exist') count += 1 else: print('Too many attempts') f.close()
4、函數進階練習題
一:編寫3個函數,每個函數執行的時間是不一樣的,
提示:可以使用time.sleep(2),讓程序sleep 2s或更多,
import time a = time.localtime() def func1(): print("%s-%s-%s"%(a.tm_year,a.tm_mon,a.tm_mday)) def func2(): time.sleep(2) print("%s-%s-%s"%(a.tm_year,a.tm_mon,a.tm_mday)) def func3(): time.sleep(4) print("%s-%s-%s"%(a.tm_year,a.tm_mon,a.tm_mday)) func1() func2() func3()
二:編寫裝飾器,為每個函數加上統計運行時間的功能
提示:在函數開始執行時加上start=time.time()就可紀錄當前執行的時間戳,函數執行結束后在time.time() - start就可以拿到執行所用時間
import time def deco(func): start_time = time.time() func() stop_time = time.time() print('the func run time is %s'%(stop_time - start_time)) def test1(): time.sleep(2) print('in the test1') def test2(): time.sleep(4) print('in the test2') deco(test1) #test1()加括號就是把它的運行結果傳進去了 deco(test2) #這種是改變了函數的調用方式 import time def timer(func): #timer(test1) func=test1 def deco(): start_time = time.time() func() #運行test1() stop_time = time.time() print('the func run time is %s'%(stop_time - start_time)) return deco def test1(): time.sleep(2) print('in the test1') def test2(): time.sleep(4) print('in the test2') #print(timer(test1)) #返回的是deco的一個內存地址 test1 = timer(test1) test1() ## 實際上運行執行的是deco #既沒有改變調用方式也沒有改變原代碼 test2 = timer(test2) test2()
三:編寫裝飾器,為函數加上認證的功能,即要求認證成功后才能執行函數
import time a = time.localtime() def logger(func): def inner(): _usename = 'kris' _passworld = 'abc123' usename = input('請輸入用戶名:') passworld = input('請輸入密碼:') if usename == _usename and passworld == _passworld: print('認證成功') func() else: print("您的輸入有誤") return inner def log1(): print("%s-%s-%s"%(a.tm_year,a.tm_mon,a.tm_mday))#13 @logger def log2(): time.sleep(2) print('%s-%s-%s' % (a.tm_year, a.tm_mon, a.tm_mday)) @logger def log3(): time.sleep(4) print('%s-%s-%s' % (a.tm_year, a.tm_mon, a.tm_mday)) log1() log2() log3()
四:編寫裝飾器,為多個函數加上認證的功能(用戶的賬號密碼來源於文件),要求登錄成功一次,后續的函數都無需再輸入用戶名和密碼
提示:從文件中讀出字符串形式的字典,可以用eval('{"name":"egon","password":"123"}')轉成字典格式
https://www.cnblogs.com/xiugeng/p/8617831.html
https://www.cnblogs.com/wangmengzhu/p/7234471.html
https://blog.csdn.net/q409561046/article/details/73346401
5、第三章總結練習
文件處理相關
1.編碼問題
-
請說明python2 與python3中的默認編碼是什么?
python2的默認編碼是ASCII碼,python3的默認編碼是utf-8
。為什么會出現中文亂碼?你能列舉出現亂碼的情況有哪幾種?(https://blog.csdn.net/ggggiqnypgjg/article/details/53271541#終端為utf-8locale為zhcngbk)
#coding:utf-8 #.py文件是什么編碼就需要告訴python用什么編碼去讀取這個.py文件。 sys.stdout.encoding,默認就是locale的編碼,print會用sys.stdout.encoding去encode()成字節流,交給terminal顯示。所以locale需要與terminal一致,才能正確print打印出中文。 sys.setdefaultencoding(‘utf8’),用於指定str.encode() str.decode()的默認編碼,默認是ascii。 以下幾種(local 為軟件運行時的語言環境): 終端為UTF-8,locale為zh_CN.GBK 終端為UTF-8,locale為zh_CN.UTF-8 終端為GBK,locale為zh_CN.GBK 終端為GBK,locale為zh_CN.UTF-8
2.如何進行編碼轉換?
字符串在python內部中是采用unicode的編碼方式,所以其他語言先decode轉換成unicode編碼,再encode轉換成utf8編碼。
3.#-*-coding:utf-8-*-
的作用是什么?
編碼聲明
4.解釋py2 bytes vs py3 bytes的區別:
Python 2 將 strings 處理為原生的 bytes 類型,而不是 unicode(python2 str == bytes), Python 3 所有的 strings 均是 unicode 類型(python3 中需要通過 unicode ) string -> encode -> bytes bytes -> decode -> string
5.文件處理
i r和rb的區別是什么?
r是只讀模式;rb是二進制讀取模式。
ii 解釋一下以下三個參數的分別作用? open(f_name,'r',encoding="utf-8")
f_name是文件;r為只讀模式;encoding是編碼
函數基礎:
- 寫函數,計算傳入數字參數的和。(動態傳參)
def calc(x,y): print('x + y') calc(3,4)
- 寫函數,用戶傳入修改的文件名,與要修改的內容,執行函數,完成整個文件的批量修改操作
def file_de(file): a = [] for line in file: b = line.capticalize() a.append(b) print(a)
寫函數,檢查用戶傳入的對象(字符串、列表、元組)的每一個元素是否含有空內容。
-
def file_k(file): n = 0 for i in file: if i ==" ": n +=1 print('有%s個空%" %n)
-
-
寫函數,檢查傳入字典的每一個value的長度,如果大於2,那么僅保留前兩個長度的內容,並將新內容返回給調用者。
dic = {"k1": "v1v1", "k2": [11,22,33,44]} PS:字典中的value只能是字符串或列表
4.閉包
在函數里邊又套了一層子函數,在子函數被返回了,就是當外層函數執行的時候子函數被返回了返回了內存地址;然后在外邊執行這個子函數的時候它又引用了外邊函數的這個變量,
相當於這個子函數跟外層函數有某種扯不清的關系,這種關系就叫閉包。
def func(): n = 10 def func2(): print('func',n) return func2 f = func() print(f) f()
函數進階:
- 寫函數,返回一個撲克牌列表,里面有52項,每一項是一個元組
-
例如:[(‘紅心’,2),(‘草花’,2), …(‘黑桃A’)]
-
def cards(): num = [] for i in range(2,11): num.append(i) num.extend(['J','Q','K','A']) type = ['紅心','草花','方塊','黑桃'] result = [] for i in num: for j in type: result.append((j,i)) return result print(cards())
-
寫函數,傳入n個數,返回字典{‘max’:最大值,’min’:最小值}
例如:min_max(2,5,7,8,4) 返回:{‘max’:8,’min’:2}
def max_min(*args): the_max = args[0] the_min = args[0] for i in args: if i > the_max: the_max = i if i < the_min: the_min = i return {'max': the_max, 'min': the_min} print(max_min(2, 4, 6, 48, -16, 999, 486, ))
-
寫函數,專門計算圖形的面積
- 其中嵌套函數,計算圓的面積,正方形的面積和長方形的面積
- 調用函數area(‘圓形’,圓半徑) 返回圓的面積
- 調用函數area(‘正方形’,邊長) 返回正方形的面積
- 調用函數area(‘長方形’,長,寬) 返回長方形的面積
def area(): def 計算長方形面積(): pass def 計算正方形面積(): pass def 計算圓形面積(): pass
import math print(''' 請按照如下格式輸出: 調用函數area(‘圓形’,圓半徑) 返回圓的面積 調用函數area(‘正方形’,邊長) 返回正方形的面積 調用函數area(‘長方形’,長,寬) 返回長方形的面積''') def area(name,*args): def areas_rectangle(x,y): return ("長方形的面積為:",x*y) def area_square(x): return ("正方形的面積為:",x**2) def area_round(r): return ("圓形的面積為:",math.pi*r*r) if name =='圓形': return area_round(*args) elif name =='正方形': return area_square(*args) elif name =='長方形': return areas_rectangle(*args) print(area('長方形', 3, 4)) print(area('圓形', 3)) print(area('正方形', 3))
-
寫函數,傳入一個參數n,返回n的階乘
例如:cal(7) 計算7*6*5*4*3*2*1
def cal(n): res= 1 for i in range(n,0,-1): # print(i) res = res*i print(res) return res print(cal(7))
-
編寫裝飾器,為多個函數加上認證的功能(用戶的賬號密碼來源於文件),要求登錄成功一次,后續的函數都無需再輸入用戶名和密碼
def login(func): def wrapper(*args,**kwargs): username = input("account:").strip() password = input("password:").strip() with open('userinfo.txt','r',encoding='utf-8') as f: userinfo = f.read().strip(',') userinfo = eval(userinfo) print(userinfo) if username in userinfo['name'] and password in userinfo['password']: print("success") else: print("pass") return wrapper @login def name(): print("hello") name()
生成器和迭代器
-
生成器和迭代器的區別?
對於list、string、tuple、dict等這些容器對象,使用for循環遍歷是很方便的。 在后台for語句對容器對象調用iter()函數。iter()是python內置函數。 iter()函數會返回一個定義了 next()方法的迭代器對象,它在容器中逐個訪問容器內的 元素。next()也是python內置函數。在沒有后續元素時,next()會拋出 一個StopIteration異常,通知for語句循環結束。 迭代器是用來幫助我們記錄每次迭代訪問到的位置,當我們對迭代器使用next()函數的 時候,迭代器會向我們返回它所記錄位置的下一個位置的數據。實際上,在使用next()函數 的時候,調用的就是迭代器對象的_next_方法(Python3中是對象的_next_方法, Python2中是對象的next()方法)。所以,我們要想構造一個迭代器, 就要實現它的_next_方法。但這還不夠,python要求迭代器本身也是可迭代的, 所以我們還要為迭代器實現_iter_方法,而_iter_方法要返回一個迭代器, 迭代器自身正是一個迭代器,所以迭代器的_iter_方法返回自身self即可。
-
生成器有幾種方式獲取value? for循環和next獲取
-
通過生成器寫一個日志調用方法, 支持以下功能
- 根據指令向屏幕輸出日志
- 根據指令向文件輸出日志
- 根據指令同時向文件&屏幕輸出日志
-
以上日志格式如下
2017-10-19 22:07:38 [1] test log db backup 3 2017-10-19 22:07:40 [2] user alex login success #注意:其中[1],[2]是指自日志方法第幾次調用,每調用一次輸出一條日志
-
代碼結構如下
def logger(filename,channel='file'): """ 日志方法 :param filename: log filename :param channel: 輸出的目的地,屏幕(terminal),文件(file),屏幕+文件(both) :return: """ ...your code... #調用 log_obj = logger(filename="web.log",channel='both') log_obj.__next__() log_obj.send('user alex login success')
內置函數
-
用map來處理字符串列表,把列表中所有人都變成sb,比方alex_sb
name=['alex','wupeiqi','yuanhao','nezha'] def sb(x): return x+'_sb' res = map(sb,name) print(list(res))
map()是 Python 內置的高階函數,它接收一個函數 f 和一個 list,並通過把
函數 f 依次作用在 list 的每個元素上,得到一個新的 list 並返回。注意:map()函數在不改變原有的lisy,而是返回一個新的list
-
用filter函數處理數字列表,將列表中所有的偶數篩選出來
num = [1,3,5,6,7,8] def func(x): if x%2 == 0: return True ret = filter(func,num) print(list(ret))
-
如下,每個小字典的name對應股票名字,shares對應多少股,price對應股票的價格
portfolio = [ {'name': 'IBM', 'shares': 100, 'price': 91.1}, {'name': 'AAPL', 'shares': 50, 'price': 543.22}, {'name': 'FB', 'shares': 200, 'price': 21.09}, {'name': 'HPQ', 'shares': 35, 'price': 31.75}, {'name': 'YHOO', 'shares': 45, 'price': 16.35}, {'name': 'ACME', 'shares': 75, 'price': 115.65} ]
計算購買每支股票的總價,用filter過濾出,單價大於100的股票有哪些?
f = filter(lambda d:d['price']>=100,portfolio) print(list(f))
1、請分別介紹文件操作中不同的打開方式之間的區別:
r rb r+ rb+ w wb w+ wb+ a ab a+ ab+
2、有列表 li = ['alex', 'egon', 'smith', 'pizza', 'alen'], 請將以字母“a”開頭的元素的首字母改為大寫字母;
li = ['alex', 'egon', 'smith', 'pizza', 'alen'] li_new = [] for i in li: if i.startswith('a'): li_new.append(i.capitalize()) else: li_new.append(i) print(li_new) for i in range(len(li)): if li[i][0] == 'a': li[i] = li[i].capitalize() else: continue print(li)
3、有如下程序, 請給出兩次調用
show_num
函數的執行結果,並說明為什么:
num = 20 def show_num(x=num): print(x) show_num() num = 30 show_num()
兩次都是20;
如果函數收到的是一個不可變對象(比如數字、字符或者元組)的引用,就不能直接修改原始對象,相當於通過“傳值’來傳遞對象,
此時如果想改變這些變量的值,可以將這些變量申明為全局變量。
4、有列表 li = ['alex', 'egon', 'smith', 'pizza', 'alen'], 請以列表中每個元素的第二個字母倒序排序;
li = ['alex', 'egon', 'smith', 'pizza', 'alen'] print(sorted(li, key=lambda x: x[1], reverse=True))
輸出
['smith', 'alex', 'alen', 'pizza', 'egon']
5、有名為poetry.txt
的文件,其內容如下,請刪除第三行;
昔人已乘黃鶴去,此地空余黃鶴樓。
黃鶴一去不復返,白雲千載空悠悠。
晴川歷歷漢陽樹,芳草萋萋鸚鵡洲。
日暮鄉關何處是?煙波江上使人愁。
方法一: import os p = 'poetry.txt' file = open(p,'r',encoding='utf-8') print(file) pnew = '%s.new'%p filenew = open(pnew,'w',encoding='utf-8') str1 = '晴川歷歷漢陽樹,芳草萋萋鸚鵡洲。' for i in file: if str1 in i: i = '' filenew.write(i) else: filenew.write(i) file.close() filenew.close() os.replace(pnew,p) 方法二:逐行讀取文件 import os f1=open('poetry.txt', 'r',encoding='utf-8') str='晴川歷歷漢陽樹,芳草萋萋鸚鵡洲。' with open('poetry1.txt', 'w', encoding='utf-8') as f2: ff1='poetry.txt' ff2='poetry1.txt' for line in f1: if str in line: line='' f2.write(line) else: f2.write(line) f1.close() f2.close() os.replace(ff2,ff1)
6、有名為username.txt
的文件,其內容格式如下,寫一個程序,判斷該文件中是否存在"alex", 如果沒有,則將字符串"alex"添加到該文件末尾,否則提示用戶該用戶已存在;
pizza
alex
egon
with open('username.txt','r+',encoding='utf-8') as f: str1 = 'alexx' i = f.read() print(i) if str1 in i: print("the user already exist in") else: f.write('\nalexx')
7、有名為user_info.txt的文件,其內容格式如下,寫一個程序,刪除id為100003的行;
pizza,100001
alex, 100002
egon, 100003
import os a = 'user_info.txt' b = 'user_info1.txt' with open(a,'r',encoding='utf-8') as f: with open(b, 'w', encoding='utf-8') as f2: for i in f: if '100003' in i: pass else: f2.write(i) os.replace(b,a)
8、有名為user_info.txt的文件,其內容格式如下,寫一個程序,將id為100002的用戶名修改為alex li
;
pizza,100001
alex, 100002
egon, 100003
file = 'user_info.txt' old_str = '100002' new_str = 'alex, 100002' file_data='' with open(file,'r',encoding='utf-8') as f1: for line in f1: if old_str in line: line =new_str file_data +=line with open(file,'w',encoding='utf-8') as f1: f1.write(file_data)
9、寫一個計算每個程序執行時間的裝飾器;
import time def timer(func): def wrapper(*args,**kwargs): start_time = time.time() func(*args) stop_time = time.time() print(stop_time-start_time) return wrapper @timer def sayhi(): print("hello word") sayhi()
10、lambda是什么?請說說你曾在什么場景下使用lambda?
lambda函數就是可以接受任意多個參數(包括可選參數)並且返回單個表達式值得函數 好處: 1.lambda函數比較輕便,即用即扔,適合完成只在一處使用的簡單功能 2.匿名函數,一般用來給filter,map這樣的函數式編程服務 3.作為回調函數,傳遞給某些應用,比如消息處理
11、題目:寫一個搖骰子游戲,要求用戶壓大小,賠率一賠一。
要求:三個骰子,搖大小,每次打印搖骰子數
import random def roll_dice(numbers=3, points=None): """ 定義骰子,循環三次 :param numbers: :param points: :return: """ print('----- 搖骰子 -----') if points is None: points = [] while numbers > 0: point = random.randrange(1, 7) points.append(point) numbers -= 1 return points def roll_result(total): """ 定義大小,三個大或者一個小兩個大。三個小或者兩個小一個大 :param total: :return: """ is_big = 11 <= total <= 18 is_small = 3 <= total <= 10 if is_big: return "big" elif is_small: return "small" def start_game(): your_money = 1000 while your_money > 0: print('----- 游戲開始 -----') choices = ["大", "小"] your_choice = input("請下注, 大 or 小") your_bet = input("下注金額:") if your_choice in choices: points = roll_dice() total = sum(points) you_win = your_choice == roll_result(total) if you_win: your_money = your_money + int(your_bet) print("骰子點數", points) print("恭喜, 你贏了%s元, 你現在的本金%s 元" % (your_bet, your_money + int(your_bet))) else: your_money = your_money - int(your_bet) print("骰子點數", points) print("很遺憾, 你輸了%s元, 你現在的本金%s 元" % (your_bet, your_money - int(your_bet))) else: print('格式有誤,請重新輸入') else: print("game over") start_game()