天天看點

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

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

1.字元串操作:

  • 解析身份證号:生日、性别、出生地等。
#擷取身份證号中的出生日期與性别
identify=input("請輸入您的身份證号:");
while(len(identify)!=18):
    print("您的身份證号碼輸入錯誤");
    identify = input("請重新輸入您的身份證号:");
year=identify[6:10];
month=identify[10:12];
day=identify[12:14];
print("出生日期是{}-{}-{}".format(year,month,day));
sex=identify[-2];
if int(sex)%2==0:
    print("性别為女");
else:
    print("性别為男")      
字元串、檔案操作,英文詞頻統計預處理
  • 凱撒密碼編碼與解碼
#凱撒密碼編碼與解碼
word=input("請輸入一段字母:");
n=input("請輸入偏移值:");
s=ord("a");
e=ord("z");
choose=input("編碼請按1,解碼請按2:");
print("凱撒密碼編碼:",end="")
for i in word:
    if s<=ord(i)<=e:
        if choose == "1":
            print(chr(s+(ord(i)-s+int(n))%26),end="");  
        elif choose == "2":
            print("凱撒密碼解碼:", end="")
            print(chr(s + (ord(i)-s-int(n)) % 26), end="");
        else:
            print("您的選擇輸入錯誤!")
    else:
        print(i,end="");      
字元串、檔案操作,英文詞頻統計預處理
字元串、檔案操作,英文詞頻統計預處理
  • 網址觀察與批量生成
#觀察淘寶搜尋頁并生成10頁的搜尋結果
print(r"淘寶搜尋‘python’結果如下");
url="https://s.taobao.com/search?q=python&imgfile=&commend=all&ssid=s5-e&search_type=item&sourceId=tb.index&spm=a21bo.2017.201856-taobao-item.1&ie=utf8&initiative_id=tbindexz_20170306"
s="&bcoffset=3&ntoffset=3&p4ppushleft=1%2C48&s="
print("第1頁網址為{}".format(url));
for i in range(9):
    arg=url+s+str(i*44);
    print("第{}頁網址為{}".format(i+2,url));      

   

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

  

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

2.英文詞頻統計預處理

  • 下載下傳一首英文的歌詞或文章或小說,儲存為utf8檔案。
  • 從檔案讀出字元串。
  • 将所有大寫轉換為小寫
  • 将所有其他做分隔符(,.?!)替換為空格
  • 分隔出一個一個的單詞
  • 并統計單詞出現的次數。
#檔案操作
def readFile():
    f=open("speech.txt");
    text=f.read();
    print(text);
    f.close();
    return text;

#文本操作
def splitText():
    dict={}
    s="124.,,"
    t=readFile().lower(); #文本中的大寫字母轉換為小寫字母
    for i in s:
        t=t.replace(i,''); #替換文本中的字元
    t = t.split(); #分割文本
    for j in t:
        dict[j]=t.count(j); #'單詞與單詞出現次數存入字典
    return dict;

#排序
def sortDict():
    d=sorted(splitText().items(),reverse=True,key=lambda d:d[1]);  '元組排序,降序,按值排序'
    print("speech文本統計詞頻如下:\n");
    for i in range(10):
       print(d[i][0],"--",d[i][1]);

def main():
    sortDict();

main();      

截圖如下:

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