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


    1.字符串操作:

    • 解析身份证号:生日、性别、出生地等。
    • 凯撒密码编码与解码
    • 网址观察与批量生成

     解析身份证号:生日、性别、出生地等

    def function3():
      print('请输入身份证号')
      ID = input()
      if len(ID) != 18:
        print('请输入有效的身份证号码')
      else:
        print('身份证号码格式正确')
      birth = ID[6:14]
      print('您的生日是:', format(birth))
      check = ID[14:17]
      if int(check) % 2 == 0:
          print('您的性别为:女')
      else:
          print('您的性别为:男')
      adress = ID[0:6]
      print('您的地址号码是:', format(adress), '可根据号码上网查')

    结果截图:

    凯撒密码编码与解码

    def function1():
      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 function2():
      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))

    结果截图:

    网址观察与批量生成

    def function4():
        for i in range(1, 10):
            url = 'http://www.baidu.com/{}.html'.format(i)
            print(url)

    结果截图:

    2.英文词频统计预处理

    • 下载一首英文的歌词或文章或小说。
    • 将所有大写转换为小写
    • 将所有其他做分隔符(,.?!)替换为空格
    • 分隔出一个一个的单词
    • 并统计单词出现的次数。

     

    def function5():
        str1="""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 that will miss you much !
    Miss you so much !"""
        #字符
        s1 = str1.lower()
        print(s1)
        # 去掉空格
        str1 = str1.lstrip()
        print(str1)
        # 将歌词的每个单词分隔组成列表形式
        print("将歌词的每个单词分隔组成列表形式:")
        strList = str1.split()
        print(strList)
        # 计算单词出现的次数
        print("计算单词出现的次数:")
        strSet = set(strList)
        for word in strSet:
            print(word, strList.count(word))

     结果截图:

    3.文件操作

    • 凯撒密码:从文件读入密函,进行加密或解密,保存到文件。
    • 词频统计:下载一首英文的歌词或文章或小说,保存为utf8文件。从文件读入文本进行处理。

    凯撒密码

     

    def function1():
        print("从文本读取到的密文是:")
    
        fo = open(r"..dmypassword", "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("写入文本中,请稍后。。。")
        fo = open(r"..dmypassword", "w")
        fo.write(str_list_encry)
        fo.close()
    
    
    def function2():
        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":
       function1()
      elif choice == "2":
        function2()
    
      else:
        print (u"您的输入有误!")

    词频统计

    import os
    path =os.getcwd()
    fo=open(r"..python1what","r")
    str1=fo.read()
    #print(fo.readline())
    fo.close()
    s1=str1.lower()
    #print(s1)
    #去掉空格
    str1=str1.lstrip()
    #print(str1)
    #将歌词的每个单词分隔组成列表形式
    print("将歌词的每个单词分隔组成列表形式:")
    strList=str1.split()
    print(strList)
    #计算单词出现的次数
    print("计算单词出现的次数:")
    strSet=set(strList)
    for word in strSet:
       print(word,strList.count(word))

  • 相关阅读:
    XML 命名空间
    XML Schema验证
    java 解析XML文档
    Java线程:创建与启动
    CCF-CSP 201312-5 I'm stuck !
    memset函数用法
    C++的字符串多行输入
    OS复习1
    os复习2
    javamail编程2
  • 原文地址:https://www.cnblogs.com/Tqin/p/10497029.html
Copyright © 2020-2023  润新知