citizenID = input("請輸入身份證号碼:")
province = citizenID[0:2]
city = citizenID[2:4]
county = citizenID[4:6]
year = citizenID[6:10]
month = citizenID[10:12]
day = citizenID[12:14]
area = citizenID[14:16]
sexCode = citizenID[16]
checkNumber = citizenID[17]
sex = "男"
if ((int(sexCode)) % 2 == 0):
sex = "女"
print("省:{},市:{},區:{},年:{},月:{},日:{},派出所:{},性别:{}, 校驗碼:{}".format(province, city, county, year, month, day, area, sex,checkNumber))
print("(注:地區編碼請參考全國地區編碼表)")
結果
字元串、檔案操作,英文詞頻統計預處理
2、凱撒密碼編碼與解碼
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("請輸入文本:")
offset = input("請輸入偏移量:")
ofs = int(offset)
if ch == 1:
result = encode(txt=inputTxt, ofs=ofs)
print("加密為:" + result)
else:
result = decode(txt=inputTxt, ofs=ofs)
print("解密為:" + result)
字元串、檔案操作,英文詞頻統計預處理
字元串、檔案操作,英文詞頻統計預處理
3、網址觀察與批量生成
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)
字元串、檔案操作,英文詞頻統計預處理
字元串、檔案操作,英文詞頻統計預處理
4、英文詞頻統計預處理
fo = open(r'G:\test\song.txt', encoding='utf-8')
text = fo.read()
fo.close()
text = text.lower() # 轉換成小寫
sep = ",.?!:''\n'"
for s in sep: # 去除各種符号,用空格代替
text = text.replace(s, ' ')
allWord = text.split(' ') # 分隔出單詞
result = [] # 存儲不重複的單詞
print("單詞 個數")
for b in allWord:
isRepeat = False
for c in result:
if b == c:
isRepeat=True
if isRepeat == False: # 若未出現在result字典裡,則加入
result.append(b)
for ch in result:
if ch != '':
print("{:<10} {:<5}".format(ch, allWord.count(ch))) # 格式化輸出