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

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

    1.字符串操作:

    • 解析身份证号:生日、性别、出生地等。
    number = "441481111111111111"
    p = number[0]+number[1]
    c = number[2]+number[3]
    q = number[4]+number[5]
    y = number[6]+number[7]+number[8]+number[9]
    m = number[10]+number[11]
    d = number[12]+number[13]
    ps = number[14]+number[15]
    s = ""if int(number[16])%2==1 else ""
    print("省份:{},城市:{},区:{},出生日期:{}年{}月{}日,派出所代码:{},性别:{}".format(p,c,q,y,m,d,ps,s))
    • 凯撒密码编码与解码
    def encryption(str):
        result = ""
        for i in str:
            result += chr(ord(i)+3);
        return result
    
    def decrypt(str):
        result = ""
        for i in str:
            result += chr(ord(i) - 3);
        return result
    print("原字符串:"+str)
    print("加密后:"+encryption(str))
    print("解密后:"+decrypt(encryption(str)))

    • 网址观察与批量生成
    for i in range(12):
        print("http://news.gzcc.cn/html/meitishijie/{}.html".format(i+1))

    2.英文词频统计预处理

    • 下载一首英文的歌词或文章或小说。
    • 将所有大写转换为小写
    • 将所有其他做分隔符(,.?!)替换为空格
    • 分隔出一个一个的单词
    • 并统计单词出现的次数。
    fo = open("seeyouagain.txt", "r", encoding='utf-8')
    song = fo.read()
    s = ",.?!"
    for i in s:
          song.replace(i,"")
    f = song.lower().split()
    for i in f:
          print(i+":"+str(f.count(i)))

    效果图如下:

    3.文件操作

    • 凯撒密码:从文件读入密函,进行加密或解密,保存到文件。
    file=open("before.txt")
    text=file.read()
    result = ""
    for i in text:
          result += chr(ord(i) + 3);
    file=open("after.txt",'w')
    file.write(result)
    file.close()

    效果图如下:

    • 词频统计:下载一首英文的歌词或文章或小说,保存为utf8文件。从文件读入文本进行处理。
    fo = open("seeyouagain.txt", "r", encoding='utf-8')
    song = fo.read()
    s = ",.?!"
    for i in s:
          song.replace(i,"")
    f = song.lower().split()
    for i in f:
          print(i+":"+str(f.count(i)))

    效果图如下:

     4.函数定义

    • 加密函数
    #凯撒加密
    def encryption(str):
          result = ""
          for i in str:
                result += chr(ord(i) + 3);
          return result
    • 解密函数
    #凯撒解密
    def decrypt(str):
          result = ""
          for i in str:
                result += chr(ord(i) - 3);
          return result
    • 读文本函数
    #读文本函数
    def read_file(file_path):
        file=open(file_path,'r')
        return file.read();
    
    
  • 相关阅读:
    关于Maya Viewport 2.0 API 开发的介绍视频
    春节大假
    Some tips about the life cycle of Maya thread pool
    Can I compile and run Dx11Shader for Maya 2015 on my side?
    How to get current deformed vertex positions in MoBu?
    想加入全球首届的 欧特克云加速计划吗?
    三本毕业(非科班),四次阿里巴巴面试,终拿 offer(大厂面经)
    mac、window版编辑器 webstorm 2016... 永久破解方法。
    node 搭载本地代理,处理web本地开发跨域问题
    js 一维数组,转成嵌套数组
  • 原文地址:https://www.cnblogs.com/grate/p/10497178.html
Copyright © 2011-2022 走看看