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

    
    

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

    1.字符串操作:

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

    def encryption(str, n): cipher = [] for i in range(len(str)): if str[i].islower(): if ord(str[i]) < 123-n: c = chr(ord(str[i]) + n) cipher.append(c) else: c = chr(ord(str[i]) + n - 26) cipher.append(c) elif str[i].isupper(): if ord(str[i]) < 91-n: c = chr(ord(str[i]) + n) cipher.append(c) else: c = chr(ord(str[i]) + n - 26) cipher.append(c) else: c = str[i] cipher.append(c) cipherstr = ('').join(cipher) return cipherstr #获得用户输入的明文 plaintext = input("请输入你的明文:") ciphertext = encryption(plaintext, 3) print(ciphertext)

      

    id=input('输入身份证号:')
    birthyear=id[6:10]
    birthmonth=id[10:12]
    birthday=id[12:14]
    sex=id[16]
    if int(sex)%2==0:
        sex='女'
    else:
        sex='男'
        print('身份证号码:{}'.format(id))
        print('出生日期:{}年{}月{}日'.format(birthyear,birthmonth,birthday))
        print('性别:{}'.format(sex))
    

      

    import urllib.request
    
    url="https://edu.cnblogs.com/campus/gzcc/GZCC-16SE1/homework/2639"
    req=urllib.request.Request(url)
    resp=urllib.request.urlopen(req)
    data=resp.read().decode('utf-8')
    
    print(data)
    

      

    2.英文词频统计预处理

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

    text=''' Say say say I come from Myrtle one brung late night melee Say say say We hung all summer sprung mattress with bae bae Say say say Trippin off Dyke and my name strike with pay day Say say say Flippin blue lighting tighting strapped with AK Say say say OG one told me one show me one eight ways How to segue Pussy power probably in headache I reminisce my life innocence Or life innocent Or life intimate with fame Like when repent Like when we make out Like when we gon get it good game She wanna hang with a Starboy The sun and the moon and star boy Astronomer anonymous I line up grind up these nine of us And 5 of us have probably fucked She mollied up I tallied up all the parts boy Twenty legs arms head head Head Head more head head Oh God bless the dead yeah I livin life high off life I wear my chokes off to bed I the greatest nigga why you so scared to say it I wanna rock I wanna rock I wanna cop more land I never stop I wanna quick advance on a bill if it ain one Break everything I a hustler came from ''' text=text.lower() text=text.split() text= text.replace(",","").replace(".","").replace(":","").replace(";","").replace("?","") print(text) s1=text.count('i') s2=text.count('say') print('i出现的次数{},say出现的次数{}'.format(s1,s2))

      

    import os,sys
    project_path=os.path.dirname(os.path.abspath(__file__))
    file_path=project_path+r'	1.txt'
    with open(file_path,'w',encoding='utf-8')as f:
        f.write('1')
    

      

    3.文件操作

    • 同一目录、绝对路径、相对路径
    • 凯撒密码:从文件读入密函,进行加密或解密,保存到文件。
    • 词频统计:下载一首英文的歌词或文章或小说,保存为utf8文件。从文件读入文本进行处理。
    import os,sys
    f=open(r"C:\UsersAdministratorDesktop2.txt","r")
    line=f.readline()
    while line:
        print(line,end="")
        line=f.readline()
    f.close()
    

      

    4.函数定义

    ·加密函数

    def JIAMI(code):
          code1=''
          for i in code:
               i=i+chr(ord(i)+3)
          return i
    

      

    ·解密函数

    def JIEMI(code):
          code1=''
          for i in code:
              i=i+chr(ord(i)-3)
          return i
    

      

    ·读文本函数

    def read(text):
         file=open(text,'r',encoding='utf8')
         return file.read()
    

      

  • 相关阅读:
    职场篇:聚焦与复盘
    职场篇:直面情绪杀手【已补更】
    .NetCore实践篇:成功解决分布式监控ZipKin聚合依赖问题(三)
    职场篇:为何我们需要思想大洗礼?
    职场篇:从温水煮青蛙说起
    .NetCore实践篇:分布式监控系统zipkin踩坑之路(二)
    postman application/json;
    yapi 个人空间 这个分组的问题
    yapi 的分组的理解!
    yapi的安装
  • 原文地址:https://www.cnblogs.com/liangqiuhua/p/10497209.html
Copyright © 2011-2022 走看看