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

本次作业要求来自于: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]))

  

posted @ 2019-03-11 21:53  梁林森  阅读(103)  评论(0编辑  收藏  举报