数据预操作

Posted fshost

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了数据预操作相关的知识,希望对你有一定的参考价值。


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
    
    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"
             }
    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):
            print(link.get(href))

    技术图片

    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)

     


    技术图片

以上是关于数据预操作的主要内容,如果未能解决你的问题,请参考以下文章

分享前端开发常用代码片段

收藏|分享前端开发常用代码片段

关于js----------------分享前端开发常用代码片段

viewpager preload会调用api来获取数据吗?

Liferay 7.3:如何预配置嵌入在页面片段中的 portlet?

mybatis以及预编译如何防止SQL注入