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)])
  • 相关阅读:
    Keras学习笔记——Hello Keras
    记一次线上事故的JVM内存学习
    postgresql中的search_path
    CentOS7安装setuptools
    CentOS7安装EPEL的两种方式
    Ncures库的介绍与安装
    CentOs6.5 安装Zlib
    Centos 安装zlib
    Windows如何压缩tar.gz格式
    nginx运行文件出错env: /etc/init.d/nginx: No such file or directory
  • 原文地址:https://www.cnblogs.com/JGaoLin/p/10507020.html
Copyright © 2011-2022 走看看