python-code-04
阿新 • • 發佈:2018-06-09
練習 文件的 pan 空格 git 註意 val print rep
函數練習:
1、寫函數,,用戶傳入修改的文件名,與要修改的內容,執行函數,完成批了修改操作
方式一:小文件修改 def change_file(file,old,new): with open(file,‘rt‘,encoding=‘utf-8‘) as read_f: data = read_f.read() data = data.replace(old,new) with open(file,‘wt‘,encoding=‘utf-8‘) as write_f: write_f.write(data) print(‘View Codesuccess‘) file_u = input(‘filename>>: ‘) old_u = input(‘old>>: ‘) new_u = input(‘new>>: ‘) change_file(file_u,old_u,new_u) 方式二:大文件修改 def change_file(file,old,new): import os file1 = file+‘.swap‘ with open(file,‘rt‘,encoding=‘utf-8‘) as read_f, open(file1,‘wt‘,encoding=‘utf-8‘) as write_f: for line in read_f: line = line.replace(old,new) write_f.write(line) os.remove(file) os.rename(file1,file) print(‘success‘) file_u = input(‘filename>>: ‘) old_u = input(‘old>>: ‘) new_u = input(‘new>>: ‘) change_file(file_u,old_u,new_u)
2、寫函數,計算傳入字符串中【數字】、【字母】、【空格] 以及 【其他】的個數
def str_count(): str_u = input(‘請輸入>>: ‘) digit_count = 0 alpha_count = 0 space_count = 0 other_count = 0 for i in str_u: if i.isdigit(): digit_count += 1 elif i.isalpha(): alpha_count += 1 elif i.isspace(): space_count += 1 else: other_count += 1 print(‘數字:%s 字母:%s 空格:%s 其他:%s‘ % (digit_count, alpha_count, space_count, other_count)) str_count()View Code
3、寫函數,判斷用戶傳入的對象(字符串、列表、元組)長度是否大於5。
def func3(seq): if len(seq) > 5: return True else: return False print(func3([1,2,3,4,5,6]))View Code
4、寫函數,檢查傳入列表的長度,如果大於2,那麽僅保留前兩個長度的內容,並將新內容返回給調用者。
def func1(seq): if len(seq) > 2: return seq[0:2] print(func1([1,2,3,4]))View Code
5、寫函數,檢查獲取傳入列表或元組對象的所有奇數位索引對應的元素,並將其作為新列表返回給調用者。
def func2(seq): return seq[::2] print(func2([1,2,3,4,5,6,7]))View Code
6、寫函數,檢查字典的每一個value的長度, 如果大於2,那麽僅保留前兩個長度的內容,並將新內容返回給調用者。
dic = {"k1": "v1v1", "k2": [11, 22, 33, 44]}
PS:字典中的value只能是字符串或列表
dic = {"k1": "v1v1", "k2": [11, 22, 33, 44]} def func6(seq): for k in seq: if len(seq[k]) > 2: seq[k] = seq[k][:2] return seq print(func6(dic))View Code
7、編寫認證功能函數,註意:後臺存儲的用戶名密碼來自於文件
def auth(): name_inp = input(‘username>>: ‘).strip() pwd_inp = input(‘password>>: ‘) with open(‘db‘,‘rt‘,encoding=‘utf-8‘) as f: for line in f: line = line.strip(‘\n‘).split(‘:‘) if name_inp == line[0] and pwd_inp == line[1]: print(‘驗證成功‘) break else: print(‘用戶名或密碼錯誤‘) auth()View Code
8、編寫註冊功能函數,將用戶的信息儲存到文件中
def func8(): name_inp = input(‘username>>: ‘).strip() pwd_inp = input(‘password>>: ‘) pwd_inp2 = input(‘password>>: ‘) if pwd_inp == pwd_inp2: print(‘註冊成功‘) with open(‘db‘,‘at‘,encoding=‘utf-8‘) as f1: f1.write(‘%s:%s\n‘ %(name_inp,pwd_inp)) else: print(‘兩次密碼不一致‘) func8()View Code
9、編寫查看用戶信息的函數,用戶的信息是事先存放於文件中的
def func9(): name = input(‘username>>: ‘).strip() with open(‘db‘,‘rt‘,encoding=‘utf-8‘) as f: for line in f: line = line.strip(‘\n‘).split(‘:‘) if name == line[0]: print(line) break else: print(‘用戶不存在‘) func9()View Code
明日默寫
1、修改文件的兩種方式
with open(‘db‘,‘rt‘,encoding=‘uf-8‘) as read_f: data = read_f.read() data = data.replace(‘old‘,‘new‘) with open(‘db‘,‘wt‘,encoding=‘utf-8‘) as write_f: write_f.write(data) import os with open(‘db‘,‘rt‘,encoding=‘utf-8‘) as f2, open(‘db.swap‘,‘wt‘,encoding=‘utf-8‘) as f3: for line in f2: f3.write(line.replace(‘old‘,‘new‘)) os.remove(‘db‘) os.rename(‘db.swap‘,‘db‘)View Code
2、註冊功能
name_inp = input(‘username>>: ‘).strip() pwd_inp = input(‘password>>: ‘) pwd_inp2 = input(‘password>>: ‘) if pwd_inp == pwd_inp2: with open(‘db‘,‘at‘,encoding=‘utf-8‘) as f: f.write(‘%s:%s\n‘ %(name_inp,pwd_inp))View Code
3、認證功能
name_inp = input(‘name>>: ‘).strip() pwd_inp = input(‘password>>: ‘) with open(‘db‘,‘rt‘,encoding=‘utf-8‘) as f: for line in f: line = line.strip(‘\n‘).split(‘:‘) if name_inp == line[0] and pwd_inp == line[1]: print(‘驗證通過‘) break else: print(‘用戶名或密碼錯誤‘)View Code
python-code-04