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

本文介绍了如何使用Python解析身份证号码以获取生日、性别及出生地信息,并详细讲解了凯撒密码的编码与解码过程,同时展示了网址观察与批量生成、英文词频统计预处理的实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

作业要求来源于: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'))

     

 

转载于:https://www.cnblogs.com/xbk6/p/10469681.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值