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

    这个作业的要求来自于:https://edu.cnblogs.com/campus/gzcc/GZCC-16SE2/homework/2646

    1.字符串操作:

    • 解析身份证号:生日、性别、出生地等。
      源代码:
    • ID=input('请输入18位身份证号码:');
      if len(ID) == 18:
          print("你输入的身份证号码为"+ID);
      else:
          print("你输入的身份证号码有误!");
      
      ID_add = ID[0:6];
      ID_birth = ID[6:14];
      ID_sex = ID[14:17];
      
      year = ID_birth[0:4];
      month = ID_birth[4:6];
      day = ID_birth[6:8];
      print("你的出生年月日为:"+year+""+month+""+day+"");
      
      if int(ID_sex)%2 == 0:
          print("性别:女");
      else:
          print("性别:男");

      实验运行结果:

    • 凯撒密码编码与解码
      plaincode=input('')
      for i in plaincode:
          print(chr(ord(i)+3),end='')
      plaincode=input('')
      s=ord('a')
      t=ord('z')
      for i in plaincode:
          if s<= ord(i)<=t:
              print(chr(s+(ord(i)-s+3)%26), end='')
          else:
              print(i,end='')
    • 实验运行结果:

    • 网址观察与批量生成
      我进行观察与批量生成的网址为广州商学院官网上的校园新闻版块,共2527页的内容。
      源代码:
    • for i in range(2,2526):
          url='http://news.gzcc.cn/html/xiaoyuanxinwen/{}.html'.format(i)
          print(url)

      实验运行结果:

    2.英文词频统计预处理

    • 下载一首英文的歌词或文章或小说,保存为utf8文件。
    • 从文件读出字符串。
    • 将所有大写转换为小写
    • 将所有其他做分隔符(,.?!)替换为空格
    • 分隔出一个一个的单词
    • 并统计单词出现的次数。
      我下载的是一篇励志的英语短片文章,将其保存为记事本的story.txt格式,并复制到了与源文件同一目录下的位置。
      源代码:
    f=open('story.txt','r',encoding='utf8')
    text=f.read()
    f.close()
    text=text.lower()
    sep=",.?!;"
    for s in sep:
        text=text.replace(s,'')
        print(text.split())
        print(text.count('man'),text.count('a'))

    实验运行结果:

  • 相关阅读:
    [转]拓扑排序
    [转]C++ string学习
    二叉树的前序遍历
    My Solution to Lowest Common Ancestor of a Binary Tree Part I(TopDown Approach)
    求二叉树节点总数
    二叉树的中序遍历
    轻松搞定面试中的二叉树题目
    VS2005 CrystalReport开发Web应用
    ASP.NET 2.0移动开发入门之使用模拟器
    [原创]Ajax UpLoadFile 多个大文件上传控件,已更新。
  • 原文地址:https://www.cnblogs.com/zyd1234/p/10471549.html
Copyright © 2011-2022 走看看