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

本次作业要求来自于:https://edu.cnblogs.com/campus/gzcc/GZCC-16SE1/homework/2684

1.字符串操作:

  • 解析身份证号:生日、性别、出生地等。
file = open('region.txt','r')
text = file.read()
file.close()
text = text.replace('\u3000','')
list = text.split('\n')
print(list)
Id=input('请输入身份证号码:')
for areaVerify in list:
        if Id[0:6] == areaVerify[0:6]:
            city = areaVerify[6:]
Birth_year = Id[6:10]
Birth_month = Id[10:12]
Birth_day = Id[12:14]
sex = '' if int(Id[16]) % 2 == 0 else ''
print('{},出生于{}年{}月{}日,{}人'.format(sex,Birth_year,Birth_month,Birth_day,city))

  • 凯撒密码编码与解码
Plaintext=input('请输入明文:')
pas=''
pla=''
print('密文为:',end='')
for i in range(0, len(Plaintext)):
    if Plaintext[i] in ['x', 'y', 'z']:
        pas += chr(ord(Plaintext[i]) - 23)
    else:
        pas += chr(ord(Plaintext[i]) + 3)
print(pas,end='')
print('\n原文为:',end='')
for i in range(0,len(pas)):
    if pas[i] in ['a','b','c']:
        pla += chr(ord(pas[i]) + 23)
    else:
        pla += chr(ord(pas[i])-3)
print(pla,end='')

  

  • 网址观察与批量生成 
for i in range(2,6):
    url='http://news.gzcc.cn/html/xiaoyuanxinwen/{}.html'.format(i)
    print(url)
import webbrowser as web
for i in range(2,6):
    web.open_new_tab('http://news.gzcc.cn/html/xiaoyuanxinwen/'+ str(i) +'.html')

  

 2.英文词频统计预处理

text='''For you I gave my life, my everything,
you were my reason why
And you were true to me,
and in our darkest hour,
your love set me free
You were the only one for me,
what love was meant to be
Forever you will be my love, all I need
Our dreams of life together gone,
I don’t know what went wrong
I will miss you forever my love
I feel so lonely without you
I don’t know what to do
Cause you were everything to me, yeah it’s true
The light that shined into my life
The fire that stirs desire
Burning out like a flame in the night
You were the only one for me,
what love was meant to be
Forever you will be my love, all I need
Our dreams of life together gone,
I don’t know what went wrong
I will miss you forever my love
And now, I walk alone
An empty heart, the precious moments gone
But I know that you are there
one day when heaven calls, I’ll see you again
when heaven calls, I’ll see you again'''
text = text.lower()
pun =  ',.?!\n'
for i in pun:
     text = text.replace(i,' ')
words = text.split(' ')
print(words)
word_count = {}
for word in words:
    if word in word_count.keys():
        word_count[word] += 1
    else:
        word_count[word] = 1
print('单词                个数')
for i in word_count:
    print('{:<20}{:<8}'.format(i,word_count[i]))

  

转载于:https://www.cnblogs.com/LinsenLiang/p/10513119.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值