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)])
  • 相关阅读:
    痞子衡嵌入式:在IAR开发环境下RTThread工程自定义函数段重定向失效分析
    痞子衡嵌入式:深扒IAR启动函数流程之段初始化实现中可用的压缩选项
    痞子衡嵌入式:深扒IAR启动函数流程及其__low_level_init设计对函数重定向的影响
    《痞子衡嵌入式半月刊》 第 44 期
    痞子衡嵌入式:再测i.MXRT1060,1170上的普通GPIO与高速GPIO极限翻转频率
    痞子衡嵌入式:把玩i.MXRT1062 TencentOS Tiny EVB_AIoT开发板(1) 开发环境搭建与点灯
    痞子衡嵌入式:嵌入式CortexM系统中断延迟及其测量方法简介
    痞子衡嵌入式:把玩i.MXRT1062 TencentOS Tiny EVB_AIoT开发板(2) 在Flash调试及离线启动
    痞子衡嵌入式:利用GPIO模块来测量i.MXRT1xxx的系统中断延迟时间
    痞子衡嵌入式:在i.MXRT1170上启动含DQS的Octal Flash可不严格设Dummy Cycle (以MT35XU512为例)
  • 原文地址:https://www.cnblogs.com/JGaoLin/p/10507020.html
Copyright © 2011-2022 走看看