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


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

    1.字符串操作

    • 解析身份证号:生日、性别、出生地等。
    • print("身份证解析")
      code=input("身份证号:")
      sf=code[0:2]
      city=code[2:4]
      county=code[4:6]
      year=code[6:10]
      month=code[10:12]
      day=code[12:14]
      print("您的出生日期为:{}年{}月{}日".format(year,month,day))
      print("您的出生地为:{}省{}市{}县".format(sf,city,county))
      

      运行截图

    • 凯撒密码编码与解码
    • new=input("请输入需要加密的内容:")
      password='';
      jiemi='';
      for i in new:
          password=password+chr(ord(i)+3);
      print("加密后:",password)
      for i in password:
          jiemi=jiemi+chr(ord(i)-3)
      print("解密内容:",new)

      运行截图

    • 网址观察与批量生成
      for i in range(2,10):
        print('https://www.baidu.com/',i)

      运行截图

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

      • 下载一首英文的歌词或文章或小说。
      • 将所有大写转换为小写
      • 将所有其他做分隔符(,.?!)替换为空格
      • 分隔出一个一个的单词
      • 并统计单词出现的次数。
      •     import string
        english='''
        During World War II, a lot of young women in Britain were in the army. Joan Phillips was one of them. She worked in a big camp, and of course met a lot of men, officers and soldiers.
          One evening she met Captain Humphreys at a dance. He said to her, "I‘m going abroad tomorrow, but I‘d be very happy if we could write to each other." Joan agreed, and they wrote for several months.
          Then his letters stopped, but she received one from another officer, telling her that he had been wounded and was in a certain army hospital in England.
          Joan went there and said to the matron, "I‘ve come to visit Captain Humphreys."
          "Only relatives are allowed to visit patients here," the matron said.
          "Oh, that‘s all right," answered Joan. "I‘m his sister."
          "I‘m very pleased to meet you," the matron said, "I‘m his mother!"
        '''
        
        for c in string.punctuation:
            english = english.replace(c," ")
        
        englishKK=english.lower().split( )
        
        count={}
        for i in englishKK:
            if i in englishKK:
                count.setdefault(i,0)
                count[i]+=1
        print(count)

        运行截图

      • 3.文件操作

        • 同一目录、绝对路径、相对路径
        • 凯撒密码:从文件读入密函,进行加密或解密,保存到文件。
        • #coding=utf-8
          file=open("abc.txt",'r')
          text=file.read();
          file.close;
          cip = '';
          for i in text:
            cip += chr(ord(i)+6)
          print("明文加密后为:",cip)
          file=open("def.txt",'w')
          file.write(cip);
          file.close()
        • 运行文档截图                                                                                                                                                                                                   

        •  保存文档截图
          • 词频统计:下载一首英文的歌词或文章或小说,保存为utf8文件。从文件读入文本进行处理。
          • file=open("abc.txt")
            text=file.read()
            
            s=',.?:;'
            for c in s:
              text = text.replace(c,'')
            
            text=text.lower()
            
            atext=text.split()
            print(atext)
            
            print("计算单词出现的次数:")
            strSet=set(text)
            for word in atext:
               print(word,text.count(word))

            运行截图                                                               

             4.函数定义

            • 加密函数
            • def jiami(abc):
                  result='';
                  for i in abc:
                result=result+chr(ord(i)+6)
                  return result
            • 解密函数
            • def jiemi(def):
                  result='';
                  for i in def:
                result=result+chr(ord(i)+6)
                  return result
            • 读文本函数
            • def read(abc):
                file=open(abc,'r')
              return file.read();
  • 相关阅读:
    ES6 常用总结(前端开发js技术进阶提升总结)
    web前端之es6对象的扩展
    ES6数组及对象遍历的新增方法 entries(),keys() 和 values()
    关于日期
    最近遇到的几个小东西
    求模
    同步 异步请求的认识
    变量名和函数名声明提升
    机顶盒前端开发小结
    js节点使用 碎片节点
  • 原文地址:https://www.cnblogs.com/LRZluck/p/10508290.html
Copyright © 2020-2023  润新知