字符串操作、文件操作,英文词频统计预处理

作业来源:https://edu.cnblogs.com/campus/gzcc/GZCC-16SE1/homework/2684

1.字符串操作:

  • 解析身份证号:生日、性别、出生地等。
    ID=input('请输入十八位身份证号码: ')
    if len(ID)==18:
      print("你的身份证号码是 "+ID)
    else:
      print("错误的身份证号码")
    year=ID[6:10]
    moon=ID[10:12]
    day=ID[12:14]
    ID_sex=ID[16:17]
    ID_check=ID[17]
    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:
      print (u"1. 加密")
      print (u"2. 解密")
      choice = input("请选择:")
      if choice == "1":
        encryption()
      elif choice == "2":
        decryption()
      else:
        print (u"您的输入有误!")

  • 网址观察与批量生成
    for i in range(2,15):
        print('http://news.gzcc.cn/html/xiaoyuanxinwen/{}.html'.format(i))

  • 2.英文词频统计预处理

    • 下载一首英文的歌词或文章或小说。
    • 将所有大写转换为小写
    • 将所有其他做分隔符(,.?!)替换为空格
    • 分隔出一个一个的单词
    • 并统计单词出现的次数
      STR='''
      Twinkle  twinkle  little star 
      How I wonder what you are 
      Up above the world so high 
      Like a diamond in the sky 
      Twinkle  twinkle  little star 
      How I wonder what you are 
      Twinkle  twinkle  little star 
      How I wonder what you are 
      Up above the world so high 
      Like a diamond in the sky 
      Twinkle  twinkle  little star 
      How I wonder what you are 
      Twinkle  twinkle  little star 
      How I wonder what you are 
      Twinkle  twinkle  little star 
      How I wonder what you are
      '''
      STR = STR.lower()#将所有大写转换为小写
      print(STR)
      s = ',.?!'
      for i in s:
         STR = STR.replace(i,' ')#将所有其他做分隔符(,.?!)替换为空格
      print(STR)
      STR = STR.split()#分隔出一个一个的单词
      print(STR)
      InfoSet = set(STR)
      Count = {}
      for word in InfoSet:
          Count.setdefault(word,STR.count(word))#统计单词出现的次数
      print(Count)

    • 3.文件操作

      • 同一目录、绝对路径、相对路径
      • 凯撒密码:从文件读入密函,进行加密或解密,保存到文件。
        def getMima():
            Massage = str(input("输入明文并保存文本:"))
            with open('excise1.txt','w') as f:
                f.write(Massage)
                f.close()
            Mima = ''
            for i in Massage:
                Mima = Mima + chr(ord(i)+2)
            print('加密结果:'+Mima+'\n')
            with open('excise2.txt','w') as f:
                f.write(Mima)
                f.close()
        
        def getMassageFromTXT():
            print("对文本内容解码..")
            with open('excise2.txt','r') as f:
                s = f.read()
            Massage = ''
            if s == None:
                print('没有可解码的文本\n')
            else:
                for i in s:
                    Massage = Massage + chr(ord(i)-2)
                print('解码结果:'+Massage+'\n')
        
        if __name__ == '__main__':
            while 1:
                a = int(input('加密(1)解码(2)退出(0):'))
                if a == 0:
                    break
                elif a == 1:
                    getMima()
                elif a == 2:
                    getMassageFromTXT()

        词频统计:下载一首英文的歌词或文章或小说,保存为utf8文件。从文件读入文本进行处理

        song = '''Twinkle  twinkle  little star 
        How I wonder what you are 
        Up above the world so high 
        Like a diamond in the sky 
        Twinkle  twinkle  little star 
        How I wonder what you are 
        Twinkle  twinkle  little star 
        How I wonder what you are 
        Up above the world so high 
        Like a diamond in the sky 
        Twinkle  twinkle  little star 
        How I wonder what you are 
        Twinkle  twinkle  little star 
        How I wonder what you are 
        Twinkle  twinkle  little star 
        How I wonder what you areound that type of environment.
        '''
        f = open('excise.txt','a',encoding='utf-8')
        f.write(song)
        f.close()

        4.函数定义

        • 加密函数
        • 解密函数
          def getMima():
              Massage = str(input("输入明文并保存文本:"))
              with open('excise1.txt','w') as f:
                  f.write(Massage)
                  f.close()
              Mima = ''
              for i in Massage:
                  Mima = Mima + chr(ord(i)+2)
              print('加密结果:'+Mima+'\n')
              with open('excise2.txt','w') as f:
                  f.write(Mima)
                  f.close()
          
          def getMassageFromTXT():
              print("对文本内容解码..")
              with open('excise2.txt','r') as f:
                  s = f.read()
              Massage = ''
              if s == None:
                  print('没有可解码的文本\n')
              else:
                  for i in s:
                      Massage = Massage + chr(ord(i)-2)
                  print('解码结果:'+Massage+'\n')
          
          if __name__ == '__main__':
              while 1:
                  a = int(input('加密(1)解码(2)退出(0):'))
                  if a == 0:
                      break
                  elif a == 1:
                      getMima()
                  elif a == 2:
                      getMassageFromTXT()

           

        • 读文本函数
          def read():
              f = open('excise.txt','r')
              s = f.read()
              print(s)
              f.close()
          
          if __name__=='__main__':
              read()

           

           

           

posted @ 2019-03-11 16:19  L杉  阅读(108)  评论(0编辑  收藏  举报