zoukankan      html  css  js  c++  java
  • 菜鸟学python之程序初体验

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

    1.字符串操作:

    • 解析身份证号:生日、性别、出生地等。
    def idption():
        str_id= input("请输入身份证号:")
        if(len(str_id)!=18):
            print("你输入的身份证号有误,请重新输入")
            idption()
        else:
            print("你出生省份为:"+str_id[0:2])
            print("你出生市区为:" + str_id[2:4])
            print("你出生县区为:" + str_id[4:6])
            print("你出生日期为:" + str_id[6:14])
            print("你出生户口派出所为:" + str_id[14:16])
            if(str_id[16:17]=='1'):
                print("你为男性且编码:" + str_id[16:17])
            else:
                print("你为女性且编码:" + str_id[16:17])
            print("校验码为:" + str_id[17:18]+"
    ")

    运行结果:

    • 凯撒密码编码与解码
    def encryption():
    
      print("导入文件中……")
    
      fo = open(r"..Linfile_textPlaintext", "r", encoding="utf-8")
      str1 = fo.read()
      print("明文为:",str1)
      fo.close()
      str_raw = str1
    
      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))
      print("保存密文……")
      te="".join(str_list_encry)
      fo = open(r"..Linfile_textCiphertext", "w")
      fo.write(te)
    
    def decryption():
        fo = open(r"..Linfile_textCiphertext", "r", encoding="utf-8")
        str1 = fo.read()
        print("密文:", str1)
        fo.close()
        str_raw = str1
    
        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))
     

    截图:

    • 网址观察与批量生成
    def webption():
        for i in range(1,5):
            url='http://news.gzcc.cn/html/xiaoyuanxinwen/{}.html'.format(i)
            print(url)

    截图:

     

    2.英文词频统计预处理

    • 下载一首英文的歌词或文章或小说。
    • 将所有大写转换为小写
    • 将所有其他做分隔符(,.?!)替换为空格
    • 分隔出一个一个的单词
    • 并统计单词出现的次数。
    def danciption():
        text='''I'm a Big big girl
    in a Big big world
    It's not a Big big thing if you leave me
    but I do do feel that
    I too too will miss you much
    miss you much...
    I can see the first leaf falling
    it's all yellow and nice
    It's so very cold outside
    like the way I'm feeling inside
    I'm a Big big girl
    in a Big big world
    It's not a big big thing if you leave me
    but I do do feel that
    I too too will miss you much
    miss you much...
    Outside it's now raining
    and tears are falling from my eyes
    why did it have to happen
    why did it all have to end
    I'm a big big girl
    in a big big world
    It's not a big big thing if you leave me
    but I do do feel that
    I too too will miss you much
    miss you much...
    I have your arms around me ooooh like fire
    but when I open my eyes
    you're gone...
    I'm a big big girl
    in a big big world
    It's not a big big thing if you leave me
    but I do do feel that
    I too too will miss you much
    miss you much...
    I'm a big big girl
    in a big big world
    It's not a big big thing if you leave me
    but I do feel I will miss you much
    miss you much...'''
        s=',.!?:'
        for c in s:
            text=text.replace(c,'')
        print(text.split())
        print("单词词频次数:")
        print("big:",text.count('big'))
        print("Big:",text.count('Big'))
        print("a:",text.count('a'))

    截图如下:

    3.文件操作

    • 同一目录、绝对路径、相对路径
    • 凯撒密码:从文件读入密函,进行加密或解密,保存到文件。
    • 词频统计:下载一首英文的歌词或文章或小说,保存为utf8文件。从文件读入文本进行处理。
    #同一路径
    f= open(r"Plaintext", 'r', encoding='utf8')
    #相对路径
    f2=open(r"..Linfile_textPlaintext", 'r', encoding='utf8')
    #绝对路径
    f3=open(r"G:PycharmProjectsLinfile_textPlaintext", 'r', encoding='utf8')
    text=f.read()
    f.close()
    t=text.find(',')
    print(text[0:t])
    print(text[t+1:len(text)])

    截图

     

     4.函数定义

    • 加密函数
    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))
    • 解密函数
    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))
    

      

    • 读文本函数
    f= open(r"..Linfile_textPlaintext", 'r', encoding='utf8')
    text=f.read()
    f.close()
    t=text.find(',')
    print(text[0:t])
    print(text[t+1:len(text)])
  • 相关阅读:
    关于C#的委托与事件的重新认识
    linux 下添加,修改,删除路由
    反射获取程序集的信息
    原创:2016.4.252016.5.1 C# informal essay and tittle_tattle
    原创:C sharp 中 Enum的几点小 Tips
    转:Dictionary<int,string>怎么获取它的值的集合?急!急!急!
    转:C#整数三种强制类型转换int、Convert.ToInt32()、int.Parse()的区别
    转:C#: static关键字的作用
    新博客 Fighting
    Win10系统下在国内访问Tensorflow官网
  • 原文地址:https://www.cnblogs.com/JGaoLin/p/10507020.html
Copyright © 2011-2022 走看看