zoukankan      html  css  js  c++  java
  • 字符串操作,文件操作,英文词频统计预处理

    作业来源: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();
  • 相关阅读:
    双态运维分享之:业务场景驱动的服务型CMDB
    双态运维分享之二: 服务型CMDB的消费场景
    双态运维:如何让CMDB配置维护更贴近人性
    CMDB经验分享之 – 剖析CMDB的设计过程
    APM最佳实践: 诊断平安城市视频网性能问题
    先定一个运维小目标,比方监控它10000台主机
    大规模Docker平台自动化监控之路
    少走冤枉路!带你走过SNMP的那些坑
    完整性约束
    数据类型
  • 原文地址:https://www.cnblogs.com/LRZluck/p/10508290.html
Copyright © 2011-2022 走看看