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

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

    1.字符串操作:

    • 解析身份证号:生日、性别、出生地等。
    
    
    id = []
    id=input('请输入身份证号:')
    if len(id)!=18:
    print('身份证号码有误')
    else:
    print("您的生日是"+id[6:14])
    sex=id[14:17]
    if int(sex)%2==0:
    print('您的性别为:女')
    else:
    print('您的性别为:男')
    sf=id[0:2]
    if int(sf)==441:
    print('广东省')
    shi=id[3]
    if int(shi)==5:
    print('汕尾'),
    qu=id[4:6]
    if int(qu)==81:
    print('陆丰')

    • 凯撒密码编码与解码
    • # 凯撒密码编码
      code=input("请您输入文字:")
      print("转化成凯撒密码为")
      for i in code:
          print(chr(ord(i)+3),end='')

    # 凯撒密码解码
    code=input("请您输入凯撒密码:")
    for i in code:
        print(chr(ord(i)-3),end='')

    • 网址观察与批量生成
    for i in range(1,11):
        url='http://news.gzcc.cn/html/2019/xiaoyuanxinwen_0305/{}.html'.format(i)
        print(url)

    2.英文词频统计预处理

    • 下载一首英文的歌词或文章或小说。
    • 将所有大写转换为小写
    • 将所有其他做分隔符(,.?!)替换为空格
    • 分隔出一个一个的单词
    • 并统计单词出现的次数。
    text='''Keeps repeating itself, what else can I say
    You wondering why I act this way
    Never should have gave you the time of day
    There must of been a gust of wind
    Cause you change your mind everytime it blows
    And you just changed it a motherfvckin again
    You just said you just wanted some dick 'fore i stuck it in
    I wouldnt of been such a prick to you
    Fvckin men you say you dont trust em
    Why do I hear the sound of toilets flushing
    Some shit is goin down
    You must've just not of been truthful from the start
    See for me it'd be nothing to say you never had my heart
    But I'd be lyin
    Fvckin see why they call this bullshit a relationship, ships sink
    And you know its love as soon as you fall in it cause shit stinks
    And it feels like everytime i fvckin do i get jynxed
    Cupid must've put a curse on me
    weeks have went buy and we only spoke twice
    I'm sittin in your driveway callin' you from the car
    Suffice I think its safe to say you're not at home
    I'm callin your cell phone you answer
    But I can tell though that you're not alone
    How was I to know it should of been time to go a long time ago
    I kept holdin on
    Why I couldnt get the hint
    You feel the draft you were throwin I wasnt catchin' your drift
    But theres a cold breeze blowin over me
    Im over you, success is the best revenge to pay you back
    And that payment is overdo'''

    #去标点符号
    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))

    3.文件操作

    • 同一目录、绝对路径、相对路径
    • 凯撒密码:从文件读入密函,进行加密或解密,保存到文件。
      file=open("a.txt")
      print('已成功在文件读取文本')
      dumi=file.read()
      kaisamima=""
      for i in dumi:
          kaisamima=kaisamima+chr(ord(i)+3)
      file = open("kaisamima.txt", 'w')
      file.write(kaisamima)
      file.close()
      print('已成功保存凯撒密码到文件!')
    • 词频统计:下载一首英文的歌词或文章或小说,保存为utf8文件。从文件读入文本进行处理。
    file=open("singe.text")
    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 bianma():
        code=input("请您输入文字:")
        print("转化成凯撒密码为")
        for i in code:
          print(chr(ord(i)+3),end='')
        return
      bianma()

    • 解密函数
    • def jiema():
        code=input("请您输入凯撒密码:")
        for i in code:
          print(chr(ord(i)-3),end='')
        return
      jiema()

    • 读文本函数
    • def duwenben():
        file=open("a.txt")
        wenben=file.read()
        print('已成功读取文本')
      b=duwenben()
  • 相关阅读:
    WPF Image控件的Source属性是一个ImageSource对象
    wx:if 与hidden
    切换远程分支
    异步请求(简单一说)
    多维数组降维方法,简单一提
    3.25发版之最后的搜索框
    wepy-城市按字母排序
    new一个新对象。。。对象???
    参数函数是对象的理解
    群辉 MariaDB 10 远程连接
  • 原文地址:https://www.cnblogs.com/JunhanLin/p/10497290.html
Copyright © 2011-2022 走看看