天天看點

資料預操作

1.字元串操作:

  • 解析身份證号:生日、性别、出生地等。
  • 凱撒密碼編碼與解碼
  • 網址觀察與批量生成

2.英文詞頻統計預處理

  • 下載下傳一首英文的歌詞或文章或小說,儲存為utf8檔案。
  • 從檔案讀出字元串。
  • 将所有大寫轉換為小寫
  • 将所有其他做分隔符(,.?!)替換為空格
  • 分隔出一個一個的單詞
  • 并統計單詞出現的次數。

上面這些是老師的。

 1

  • ID = input('請輸入十八位身份證号碼: ')
    if len(ID) == 18:
        print("你的身份證号碼是 " + ID)
    else:
        print("錯誤的身份證号碼")
    
    ID_add = ID[0:6]
    ID_birth = ID[6:14]
    ID_sex = ID[14:17]
    ID_check = ID[17]
    year = ID_birth[0:4]
    moon = ID_birth[4:6]
    day = ID_birth[6:8]
    print("生日: " + year + '年' + moon + '月' + day + '日')
    
    if int(ID_sex) % 2 == 0:
        print('性别:女')
    else:
        print('性别:男')
    
    W = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2]
    ID_num = [18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2]
    ID_CHECK = ['1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2']
    ID_aXw = 0
    for i in range(len(W)):
        ID_aXw = ID_aXw + int(ID[i]) * W[i]
    ID_Check = ID_aXw % 11
    if ID_check == ID_CHECK[ID_Check]:
        print('正确的身份證号碼')
    else:
        print('錯誤的身份證号碼')      
  • import os
    def encryption():
        str_raw = input("請輸入明文:")
        k = int(input("請輸入位移值:"))
        str_change = str_raw.lower()
        str_list = list(str_change)
        str_list_encry = str_list
        i = 0
        while i < len(str_list):
            if ord(str_list[i]) < 123-k:
                str_list_encry[i] = chr(ord(str_list[i]) + k)
            else:
                str_list_encry[i] = chr(ord(str_list[i]) + k - 26)
            i = i+1
        print ("加密結果為:"+"".join(str_list_encry))
    def decryption():
        str_raw = input("請輸入密文:")
        k = int(input("請輸入位移值:"))
        str_change = str_raw.lower()
        str_list = list(str_change)
        str_list_decry = str_list
        i = 0
        while i < len(str_list):
            if ord(str_list[i]) >= 97+k:
                str_list_decry[i] = chr(ord(str_list[i]) - k)
            else:
                str_list_decry[i] = chr(ord(str_list[i]) + 26 - k)
            i = i+1
        print ("解密結果為:"+"".join(str_list_decry))
    while True:
            encryption()
            decryption()
            choice = input("continue? Y/N")
            if choice == "Y":
                continue;
            else:
                exit(0)
    
          
    資料預操作
  • bilibili熱門視訊連結   
    import requests
    from bs4 import BeautifulSoup
    import os
    
    url = 'https://www.bilibili.com/ranking/all/4/1/7/?spm_id_from=333.334.b_72616e6b696e675f67616d65.15'
    
    parser = 'html.parser'
    cur_path = os.getcwd() + '/'
    
    header = {
        'User-Agent':'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.104 Safari/537.36',  
        "Accept-Language":"zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3",
        "Accept-Encoding":"gzip, deflate",
        "Referer":"http://bilibili.com/",
        "Cookie":"safedog-flow-item=; bdshare_firstime=1504530621757",
        "Connection":"keep-alive",
        "Upgrade-Insecure-Requests":"1",
        "Cache-Control":"max-age=0"
             }
    preview_page_cnt = 2
    for cur_page in range(1, int(preview_page_cnt) + 1):
        cur_url = url + str(cur_page)
        cur_page = requests.get(cur_url, headers=header)
        soup = BeautifulSoup(cur_page.text, parser)
        for link in soup.find_all('a',target='_blank')[1::2]:
            dir_name = link.get_text().strip().replace('?', '')
            link=link['href']
            print(link+dir_name)      
    資料預操作

    2.

    歌曲

    Stand up, all victims of oppression
      For the tyrants fear your might
      Don't cling so hard to your possessions
      For you have nothing, if you have no rights
      Let racist ignorance be ended
      For respect makes the empires fall
      Freedom is merely privilege extended
      Unless enjoyed by one and all.
      
      So come brothers and sisters
      For the struggle carries on
      The Internationale
      Unites the world in song
      So comrades come rally
      For this is the time and place
      The international ideal
      Unites the human race
      
      Let no one build walls to divide us
      Walls of hatred nor walls of stone
      Come greet the dawn and stand beside us
      We'll live together or we'll die alone
      In our world poisoned by exploitation
      Those who have taken, now they must give
      And end the vanity of nations
      We've one but one Earth on which to live
      
      So come brothers and sisters
      For the struggle carries on
      The Internationale
      Unites the world in song
      So comrades come rally
      For this is the time and place
      The international ideal
      Unites the human race
      
      And so begins the final drama
      In the streets and in the fields
      We stand unbowed before their armour
      We defy their guns and shields
      When we fight, provoked by their aggression
      Let us be inspired by life and love
      For though they offer us concessions
      Change will not come from above.
      
      So come brothers and sisters
      For the struggle carries on
      The Internationale
      Unites the world in song
      So comrades come rally
      For this is the time and place
      The international ideal
      Unites the human race
          
      代碼
    resoult={}
    str=",.?!  "
    op=0
    with open("pa.txt","r",encoding="utf8",errors="ignore") as  f:
        text=f.read()
    for i in range(4):
        text=text.replace(str[i]," ")
        print(text)
    for i in range(100):
        alist = text.split(" ")
        result=text.count(alist[i])
        print(alist[i])
        print(result)      
    資料預操作