1. 程式人生 > >5.1.3 案例精選

5.1.3 案例精選

sin org 數據 inf utf result adl ges 郵箱

  5-1 編寫函數實現字符串加密和解密,循環使用指定秘鑰,采用簡單的異或算法。

 1 def crypt(source,key):
 2     from itertools import cycle
 3     result = ‘‘
 4     temp=cycle(key)
 5 
 6     for ch in source:
 7         result = result + chr( ord(ch)^ord(next(temp)) )
 8 
 9     return result
10 
11 source = Shandong Institute of Business and Technology
12 key = Dong Fuguo 13 14 print(Before Encrypted:,source) 15 encrypted = crypt(source,key) 16 print(After Encrypted:,encrypted) 17 decrypted=crypt(encrypted,key) 18 print(After Decrypted:,decrypted) 19 20 #Before Encrypted: Shandong Institute of Business and Technology 21 #After Encrypted:  D) U&*T3U "O,S/d  + Y
22 #After Decrypted: Shandong Institute of Business and Technology

  5-2 編寫程序,生成大量隨機信息

    本例代碼演示了如何使用Python標準庫random來生成隨機數據,這在需要獲取大量數據來測試或演示軟件的時候非常有用,不僅能真實展示軟件功能或算法,還可以避免泄露真實數據或者引起不必要的爭議。

  1 import random
  2 import string
  3 import codecs
  4 
  5 #常用漢字 Unicode 編碼表(部分),完整列表詳見配套源代碼
  6 stringBase = 
\u7684\u4e00\u4e86\u662f\u6211\u4e0d\u5728\u4eba 7 8 #轉換為漢字 9 stringBase = ‘‘.join(stringBase.split(\\u)) 10 #print(stringBase) #的一了是我不在人 11 12 #獲取郵箱 13 def getEmail(): 14 15 #常見域名後綴,可以隨意擴展該列表 16 suffix = [.com,.org,.net,.cn] 17 characters = string.ascii_letters + string.digits+_ 18 19 #獲取郵箱用戶名 20 username = ‘‘.join((random.choice(characters) for i in range(random.randint(6,12)))) 21 22 #獲取郵箱域名 23 domain = ‘‘.join((random.choice(characters) for i in range(random.randint(3,6)))) 24 25 return username + @ + domain + random.choice(suffix) 26 27 #獲取手機號碼 28 def getTelNo(): 29 return ‘‘.join((str(random.randint(0,9)) for i in range(11))) 30 31 #獲取用戶名或地址 32 def getNameOrAddress(flag): 33 ‘‘‘flag=1 表示返回隨機姓名,flag = 0表示返回隨機地址‘‘‘ 34 result = ‘‘ 35 if flag == 1: 36 #大部分中國人的姓名為2-4個漢字 37 rangestart,rangeend = 2 , 5 38 elif flag ==0: 39 #假設地址在10-31個漢字之間 40 rangestart,rangeend = 10,31 41 else: 42 print(flag must be 1 or 0) 43 return ‘‘ 44 45 for i in range(rangestart,rangeend): 46 result += random.choice(stringBase) 47 48 return result 49 50 def getSex(): 51 return random.choice((,)) 52 53 def getAge(): 54 return str(random.randint(18,100)) 55 56 def main(filename): 57 with codecs.open(filename,w,utf-8) as fp: 58 fp.write(Name,-Sex,Age,---TelNo--,-------------------Address----------------,-----Email----- \n) 59 60 #隨機生成200個人的信息 61 for i in range(10): 62 name = getNameOrAddress(1) 63 sex = getSex() 64 age = getAge() 65 tel = getTelNo() 66 address = getNameOrAddress(0) 67 email = getEmail() 68 line = ,.join([name,sex,age,tel,address,email]) + \n 69 fp.write(line) 70 71 72 def output(filename): 73 with codecs.open(filename,r,utf-8) as fp: 74 while True: 75 line = fp.readline() 76 if not line: 77 return 78 line = line.split() 79 for i in line: 80 print(i,end=,) 81 print() 82 83 if __name__ == __main__: 84 filename = infomation.txt 85 main(filename) 86 output(filename) 87 88 ‘‘‘ 89 Name,-Sex,Age,---TelNo--,-------------------Address----------------,-----Email-----, 90 是的在,男,35,40020447105,一我了了我是是人了的了我在我的不了的人了我,[email protected], 91 一是我,女,44,28056977003,了我人不在的人不不是的在不人我是我不人一的,[email protected], 92 了在在,女,20,08361670393,人一了了是是在一是的人一在不在是不的了了是,[email protected], 93 一一是,女,68,99960031767,了的一我我人的在我的一一不了是了了我在人不,[email protected], 94 一人了,男,55,33382346683,人的我在不人了了是我是是了了在我人在我一我,[email protected], 95 的了不,男,95,90126113931,一我人了的不人我在了的了了人人是不了的一的,[email protected], 96 一是是,女,26,78260102051,的人人我了是的的了是了我人不的一不人我是我,[email protected], 97 的的人,男,30,73940532426,的一在人是了的不了是不的在在人不人是在我了,[email protected], 98 不在的,女,61,10389727006,了人我了的了的不了在一一我是是我了人在是在,[email protected], 99 是不了,男,90,59993303692,人是了在了了一人人是我是是的我一我了了是一,[email protected], 100 ‘‘‘

  拓展知識:Python擴展庫jieba和snownlp很好地支持了中文分詞,可以使用pip命令進行安裝。在自然語言處理領域經常需要對文字進行分詞,分詞的準確度直接影響了後續文本處理和挖掘算法的最終效果。

  拓展知識:Python擴展庫pypinyin支持漢字到拼音的轉換,並且可以和分詞擴展庫配合使用。

5.1.3 案例精選