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

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

一、字符串操作

(一)基本要求

  • 解析身份证号:生日、性别、出生地等。
  • 凯撒密码编码与解码
  • 网址观察与批量生成

(二)编码实现

1、解析身份证

sfz=[]
'''存放身份证前两位对应的省名'''
placeNum={'11':'北京市','12':'天津市','13':'河北省','14':'山西省','15':'内蒙古自治区','21':'辽宁省','22':'吉林省','23':'黑龙江省','31':'上海市','32':'江苏省','33':'浙江省','34':'安徽省','35':'福建省','36':'江西省','37':'山东省',
          '41':'河南省','42':'湖北省','43':'湖南省','44':'广东省','45':'广西壮族自治区','46':'海南省','50':'重庆市','51':'四川省','52':'贵州省','53':'云南省','54':'西藏自治区','61':'陕西省','62':'甘肃省','63':'青海省','64':'宁夏回族自治区',
          '65':'新疆维吾尔自治区','71':'台湾省','81':'香港特别行政区','91':'澳门特别行政区',}

print('身份证解析小程序~')
sfz=input('请输入您的身份证号码:')
print('您的身份证号码为:'+sfz)
print('解析开始......')

'''出生年月日'''
bir=[]
for i in sfz[6:14]:
    bir.append(i)
birY=''.join(bir[0:4])
birM=''.join(bir[4:6])
birD=''.join(bir[6:8])

'''出生地'''
place=placeNum[''.join(sfz[0:2])]

'''判断性别'''
sexNum=int(sfz[16])
if sexNum%2 == 0:
    sex='girl'
else:
    sex='boy'

'''输出信息'''
print('Hi,'+sex+',我知道你是'+birY+''+birM+''+birD+'日出生的'+place+'人哦!')

 

2、凯撒密码

print('下面是凯撒加密')
mw=input('请输入您需要加密的明文:')
MiMa=''
jiemi=''for i in mw:
    MiMa=MiMa+chr(ord(i)+3)
print('加密后的密码为:',MiMa)

 

3、网址观察与生成

print('下面是校园新闻~')
for i in range(2,15):
    print('http://news.gzcc.cn/html/xiaoyuanxinwen/{}.html'.format(i))

 

(三)截图展示

1、解析身份证

2、凯撒密码

 

3、网址观察与生成

 

二、英文词频统计预处理

(一)基本要求

  • 下载一首英文的歌词或文章或小说。
  • 将所有大写转换为小写
  • 将所有其他做分隔符(,.?!)替换为空格
  • 分隔出一个一个的单词
  • 并统计单词出现的次数。

(二)编码实现

import string
song='''
Every night in my dreams
I see you,I feel you
That is how I know you go on
Far across the distance
And spaces between us
You have come to show you go on
Near far
Wherever you are
I believe
That the heart does go on
Once more you open the door
And you're here in my heart
And my heart will go on and on
Love can touch us one time
And last for a lifetime
And never let go till we're gone
Love was when I loved you
One true time I hold to
In my life well always go on
Near far
Wherever you are
I believe
That the heart does go on
Once more you open the door
And you're here in my heart
And my heart will go on and on
you're here
There's nothing I fear
And I know
That my heart will go on
We'll stay forever this way
You are safe in my heart
And my heart will go on and on
'''

for c in string.punctuation:
    song = song.replace(c," ")

'''歌词转为小写并分成单词'''
songDC=song.lower().split( )

'''字典'''
count={}
for i in songDC:
    if i in songDC:
        count.setdefault(i,0)
        count[i]+=1
print(count)

 

(三)截图展示

三、文件操作

(一)基本要求

1、凯撒密码:从文件读入密函,进行加密或解密,保存到文件。

2、词频统计:

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

(二)编码实现

1、凯撒密码

print('下面是凯撒加密')
file=open("mw.txt")
mw=file.read()
MiMa=''
jiemi=''
for i in mw:
    MiMa=MiMa+chr(ord(i)+3)
file = open("MiMa.txt", 'w')
file.write(MiMa)
file.close()
print('成功保存凯撒密码!')

 

2、词频统计

import string
file=open("song.txt")
song=file.read()

for c in string.punctuation:
    song = song.replace(c," ")

'''歌词转为小写并分成单词'''
songDC=song.lower().split( )

'''列表转集合去重'''
count={}
for i in songDC:
    if i in songDC:
        count.setdefault(i,0)
        count[i]+=1
print(count)

 

(三)截图展示

1、凯撒密码

 

2、词频统计

posted @ 2019-03-09 23:56  阿叔  阅读(264)  评论(0编辑  收藏  举报