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();
    
    
  • 相关阅读:
    mysql的存储过程
    一份php高级工程师的面试题,我每天看一点点
    php的常用函数(持续更新)
    php 中文字符串截取
    php递归遍历文件目录
    ajax timeout 断网处理
    angular 刷新问题
    angular state中templateUrl 路径的模板
    angular请求传递不了数据
    截取字符串 substring substr slice
  • 原文地址:https://www.cnblogs.com/grate/p/10497178.html
Copyright © 2011-2022 走看看