字符串操作,英文词频统计预处理

作业要求来源于:https://edu.cnblogs.com/campus/gzcc/GZCC-16SE2/homework/2646
解析身份证号:生日、性别、出生地等。

#
! /use/bin/python # -*- coding: UTF-8 -*- def check_idcard(id_card): if id_card is None or id_card.strip() =='': print('请输入身份证信息') exit(-1) if len(id_card) !=18: print('身份证只有18位') print('地址信息:%s'% id_card[0:6]) print('生日信息:%s' % id_card[0:14]) print('后4位校验码:%s' % id_card[14:18]) if __name__=='__mian__': check_idcard('450403197503271234')

凯撒密码编码与解码:

#-*-coding:utf-8-*-
__author__ = 007
__date__ = 2019 / 03/ 04
def encryption():
    str_raw = raw_input("请输入明文:")
    k = input("请输入位移值:")
    str_change = str_raw.lower()
    str_list = list(str_change)
    str_list_encry = str_list
    i = 0
    while i < len(str_list):
        if ord(str_list[i]) < 123-k:
            str_list_encry[i] = chr(ord(str_list[i]) + k)
        else:
            str_list_encry[i] = chr(ord(str_list[i]) + k - 26)
        i = i+1
    print "加密结果为:"+"".join(str_list_encry)

def decryption():
    str_raw = raw_input("请输入密文:")
    k = input("请输入位移值:")
    str_change = str_raw.lower()
    str_list = list(str_change)
    str_list_decry = str_list
    i = 0
    while i < len(str_list):
        if ord(str_list[i]) >= 97+k:
            str_list_decry[i] = chr(ord(str_list[i]) - k)
        else:
            str_list_decry[i] = chr(ord(str_list[i]) + 26 - k)
        i = i+1
    print "解密结果为:"+"".join(str_list_decry)

while True:
    print u"1. 加密"
    print u"2. 解密"
    choice = raw_input("请选择:")
    if choice == "1":
        encryption()
    elif choice == "2":
        decryption()
    else:
        print u"您的输入有误!"

#if __name__ == "__main__":
#    main

网址观察与批量生成:

for i in range(6, 254):
    url = 'http://news.gzcc.cn/html/xiaoyuanxinwen/{}.html'.format(i)
print(url)

2.英文词频统计预处理

  • 下载一首英文的歌词或文章或小说,保存为utf8文件。
  • 从文件读出字符串。
  • 将所有大写转换为小写
  • 将所有其他做分隔符(,.?!)替换为空格
  • 分隔出一个一个的单词
  • 并统计单词出现的次数。
  • text="Mr. Johnson had never been up in an aerophane before and he had read a lot about air accidents," \
         " so one day when a friend offered to take him for a ride in his own small phane, " \
         "Mr. Johnson was very worried about accepting." \
         " Finally, however, his friend persuaded him that it was very safe, and Mr. Johnson boarded the plane."
    print(text.lower())
    str=", . ! ? - ' :"
    for s in  str:
        text=text.replace(s,'')
        print(text.split())
        print(text.count('you'),text.count('her'))

     

 

posted @ 2019-03-04 16:42  xbk6  阅读(154)  评论(0编辑  收藏  举报