1.字元串操作:
- 解析身份證号:生日、性别、出生地等。
import des as des if __name__ == '__main__': def sex(str1): # 檢視性别 if int(str1[-2]) % 2 == 0: return '女' else: return '男' idDes = ['廣東省廣州市市轄區', '廣東省廣州市東山區', '廣東省廣州市荔灣區', '廣東省廣州市越秀區', '廣東省廣州市海珠區', '廣東省廣州市天河區'] idNum = input('輸入十八位(廣州)身份證号碼:') brith = idNum[6:14] # test = idNum[:4] if (idNum[:4] == '4401'): if (idNum[4:6] == '01'): test =idNum[4:6] des = idDes[0] if (idNum[4:6] == '02'): des = idDes[1] if (idNum[4:6] == '03'): des = idDes[2] if (idNum[4:6] == '04'): des = idDes[3] if (idNum[4:6] == '05'): des = idDes[4] if (idNum[4:6] == '06'): des = idDes[5] # elif des == 0: # print('資訊未錄入') print('身份證歸屬地:',des) print('生日:',brith) print('性别:',sex(idNum))
- 凱撒密碼編碼與解碼
if __name__ == '__main__':choose = input("請選擇: 1.加密 2.解密 :") ch = int(choose) # 加密 def encode(txt, ofs): t = "" for ch in txt: if 'a' <= ch <= 'z': t += chr(ord('a') + ((ord(ch) - ord('a')) + ofs) % 26) elif 'A' <= ch <= 'Z': t += chr(ord('A') + ((ord(ch) - ord('A')) + ofs) % 26) else: t += ch return t # 解密 def decode(txt, ofs): t = "" for ch in txt: if 'a' <= ch <= 'z': t += chr(ord('a') + ((ord(ch) - ord('a')) - ofs) % 26) elif 'A' <= ch <= 'Z': t += chr(ord('A') + ((ord(ch) - ord('A')) - ofs) % 26) else: t += ch return t inputTxt = input("請輸入:") ofs = 3 if ch == 1: result = encode(txt=inputTxt, ofs=ofs) print("加密為:" + result) else: result = decode(txt=inputTxt, ofs=ofs) print("解密為:" + result)
- 網址觀察與批量生成
if __name__ == '__main__':import webbrowser as web url = "http://news.gzcc.cn/html/xiaoyuanxinwen/2.html" web.open_new_tab(url) for i in range(3, 10): url = 'http://news.gzcc.cn/html/xiaoyuanxinwen/' + str(i) + '.html' web.open_new_tab(url) print(url)
2.英文詞頻統計預處理
- 下載下傳一首英文的歌詞或文章或小說,儲存為utf8檔案。
- 從檔案讀出字元串。
- 将所有大寫轉換為小寫
- 将所有其他做分隔符(,.?!)替換為空格
- 分隔出一個一個的單詞
- 并統計單詞出現的次數。
if __name__ == '__main__':fo = open("test.txt", 'r')
#讀取英文文章
text = fo.read()
#大寫轉小寫
text = text.lower()
print(text+"\n")
strs = {",", '.', "?", "!", ';'}
#替換符号為空格
for str in strs:
text = text.replace(str, "")
#分割單詞
text = text.split()
print(text)
#統計詞頻
textS = {}
for i in text:
count = text.count(i)
textS[i] = count
textS = sorted(textS.items(), key=lambda text:text[1])
print(textS)