天天看點

字元串、檔案操作,英文詞頻統計預處理

作業要求:https://edu.cnblogs.com/campus/gzcc/GZCC-16SE2/homework/2646

1.字元串操作

  解析身份證号:生日、性别、出生地等

1 # -*- coding:utf-8 -*-
 2 idCard=input("輸入身份證号碼:")
 3 local=idCard[0:2]
 4 year= idCard[6:10]
 5 month= idCard[10:12]
 6 day= idCard[12:14]
 7 sex=idCard[16:17]
 8 address={'11':'北京市','12':'天津市','13':'河北省','14':'山西省','15':'内蒙古自治區','21':'遼甯省','22':'吉林省','23':'黑龍江省','31':'上海市','32':'江蘇省','33':'浙江省','34':'安徽省','35':'福建省','36':'江西省','37':'山東省','41':'河南省','42':'湖北省','43':'湖南省','44':'廣東省','45':'廣西壯族自治區','46':'海南省','50':'重慶市','51':'四川省','52':'貴州省','53':'雲南省','54':'西藏自治區','61':'陝西省','62':'甘肅省','63':'青海省','64':'甯夏回族自治區','65':'新疆維吾爾自治區','71':'台灣省','81':'香港特别行政區','82':'澳門特别行政區'}
 9 print("地區:" + address.get(local),"生日:{}年{}月{}日".format(year, month, day))
10 if(int(sex)%2==0):
11     print("性别:女")
12 else:
13     print("性别:男")      

運作結果:

字元串、檔案操作,英文詞頻統計預處理

  

  凱撒密碼編碼與解碼

1 def decode():
 2     code = input("請輸入原碼:\n")
 3     decode = ""
 4     for n in code:
 5         if -128 >= ord(n) or 127 <= ord(n):
 6             print("原碼取值超出範圍!!!")
 7             break
 8         decode += chr(ord(n) + 3)
 9 
10     print("原碼:" + code + "\n")
11     print("編碼:" + decode + "\n")
12     return
13 
14 
15 def encode():
16     decode = input("請輸入編碼:\n")
17     encode = ""
18     for n in decode:
19         if -125 >= ord(n) or 130 <= ord(n):
20             print("編碼取值超出範圍!!!")
21             break
22         encode += chr(ord(n) - 3)
23 
24     print("編碼:" + decode + "\n")
25     print("譯碼:" + encode + "\n")
26     return
27 
28 
29 while 1:
30     choose = input("請選擇功能:\n 1、編碼\n 2、譯碼\n")
31     if choose == "1":
32         decode()
33     elif choose == "2":
34         encode()
35     else:
36         print("\n\n請重新輸入:\n\n")      
字元串、檔案操作,英文詞頻統計預處理

網址觀察與批量生成

1 for i in range(2,30):
2     url='https://edu.cnblogs.com/homework?page={}'.format(i)
3     print(url)      
字元串、檔案操作,英文詞頻統計預處理

2.英文詞頻統計

1 import re
 2 
 3 file = open("two tigers.txt", "r+")
 4 str = file.read()
 5 print("讀取的字元串是 : \n", str)
 6 
 7 str = re.sub('[\r\n\t,]', ' ', str)
 8 words = str.split(" ")
 9 
10 single = []
11 
12 for word in words:
13     if (word not in single):
14         single.append(word)
15 
16 i = 1
17 while single:
18     print(single[0], "出現次數:", words.count(single[0]), end="     ")
19     single.pop(0)
20 
21     if i % 4 == 0:
22         print("\n")
23 
24     i += 1
25 
26 file.close()      
字元串、檔案操作,英文詞頻統計預處理