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

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

     

    1.字符串操作

    • 解析身份证号:生日、性别、出生地等
    #解析广州居民身份证号
    str=input("请输入你的广州居民身份证号:")
    gz=str[0:4]#
    dq=str[4:6]#
    year=str[6:10]#出生年
    month=str[10:12]#出生月
    day=str[12:14]#出生日
    sex=str[14:17]#性别
    j=0
    if len(str) == 18:#验证身份证长度是否正确
        if gz != '4401':
            print('该身份证号不属于广州居民!')
        else:
            num = ['03', '04', '05', '06', '11', '12', '13', '14', '84', '83', '15', '16']
            diqu = ['荔湾区', '越秀区', '海珠区', '天河区', '白云区', '黄埔区', '番禺区', '花都区', '从化区', '增城区', '南沙区', '萝岗区']
            for i in num:
                if dq == i:#比较地区是否存在
                    if int(sex) % 2 == 0:#验证性别
                        xingbie=''
                    else:
                        xingbie=''
                    print('你住在广州市{},出生于{}年{}月{}日,性别为{}。'.format(diqu[j],year,month,day,xingbie))
                    break
                else:
                    j=j+1
            if j>len(diqu):#数组内已查找完无结果
                print('该身份证号有误!')
    else:
        print('你的身份证号码有误!')
    解析身份证号具体代码

    详情结果如下:

    • 凯撒密码编码与解码
    def encryption():
      str_raw = input("请输入明文:")
      s=ord('a')
      t=ord('z')
      print('加密结果为:')
      for i in str_raw:
        if s<=ord(i)<=t:
          print(chr(s+(ord(i)-s+3)%26),end='')#往后移三位
        else:
          print(i,end='')
    def decryption():
      str_raw = input("请输入密文:")
      s=ord('a')
      t=ord('z')
      print('加密结果为:')
      for i in str_raw:
         if s<=ord(i)<=t:
          print(chr(s+(ord(i)-s-3)%26),end='')#往前移三位
         else:
          print(i,end='')
    while True:
      print (u"1. 加密")
      print (u"2. 解密")
      choice = input("请选择:")
      if choice == "1":
        encryption()
      elif choice == "2":
        decryption()
      else:
        print (u"您的输入有误!")
      print('
    ')
    凯撒密码具体代码

    详情结果如下:

     

    • 网址观察与批量生成
    for i in range(2,10):
        url='http://news.gzcc.cn/html/xiaoyuanxinwen/{}.html'.format(i)
        print(url)
    网址批量生成具体代码

    详细结果如下:

     2.英文词频统计预处理

    • 下载一首英文的歌词或文章或小说,保存为utf8文件。

    • 从文件读出字符串。
    • 将所有大写转换为小写
    • 将所有其他做分隔符(,.?!)替换为空格
    • 分隔出一个一个的单词
    • 并统计单词出现的次数。
    f=open(r'shape of you.txt','r')#打开相对路径里的文本
    text=f.read()#读取内容存到变量里
    print(text)#输出文本内容
    f.close()
    lowerText=text.lower()#将文本里所有字母转化为小写
    sep=',?.!-:_'
    for s in sep:
        text=text.replace(s,' ')#字符串里的符号转变成空格
    print(lowerText.split())#按空格分隔单词
    print(text.count('come'),text.count('Come'),lowerText.count('come'))#统计原文的come和Come的词频,统计转为小写文本的come次数
    英文词频统计具体代码

    详细结果如下:

     

  • 相关阅读:
    java8学习之Collector复合与注意事项
    动画学习之WIFI图形绘制
    java线程基础巩固---多线程死锁分析,案例介绍
    java8学习之Collector同一性与结合性分析
    java8学习之Collector源码分析与收集器核心
    java8学习之Stream分组与分区详解
    kotlin面向对象之抽象类、继承、多态
    matplotlib-曲线和折线案例
    人口、人口密度分析项目-条形图
    开机自启:bat实现一次性打开win7中的常用软件和文件夹
  • 原文地址:https://www.cnblogs.com/kunnkkk/p/10471642.html
Copyright © 2011-2022 走看看