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

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

一、字符串操作

(一)基本要求

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

(二)编码实现

1、解析身份证

id=[]
'''存放身份证前两位对应的省名'''
place={'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':'澳门特别行政区',}


id=input('请输入您的身份证号码:')
print('waiting......')

#出生年月日
bir=[]
for i in id[6:14]:
    bir.append(i)
year= ''.join(bir[0:4])
morth= ''.join(bir[4:6])
day= ''.join(bir[6:8])

#出生地
place=place[''.join(id[0:2])]

#性别
sexNum=int(id[16])
if sexNum%2 == 1:
    sex='帅哥'
else:
    sex='美女'

#输出信息
print('性别:' + sex +',出生:' + year + '' + morth + '' + day + '' + place + '')

 

2、凯撒加密

Str = input('请输入;')
for line in Str:
        print('{}'.format(chr(ord(line) +3)), end="")

 

3、网址观察

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

(三)截图展示

1、解析身份证

2、凯撒加密

3、网址观察

二、英文词频统计预处理

(一)基本要求

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

(二)编码实现

text = '''
Well I wonder could it be
  When I was dreaming about you baby
  You were dreaming of me
  Call me crazy
  Call me blind
  To still be suffering is stupid after all of this time

  Did I lose my love to someone better
  And does she love you like I do
  I do, you know I really really do

  Well hey
  So much I need to say
  Been lonely since the day
  The day you went away

  So sad but true
  For me there's only you
  Been crying since the day
  the day you went away

  I remember date and time
  September twenty second
  Sunday twenty five after nine
  In the doorway with your case
  No longer shouting at each other
  There were tears on our faces

  And we were letting go of something special
  Something we'll never have again
  I know, I guess I really really know
'''
text1 = text.lower()
text2 =text1.split()
num =text2.count('i')
print(text2)
print('“i”出现的次数:',num)

(三)截图展示

三、文件操作

(一)基本要求

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

2、词频统计:

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

(二)编码实现

1、凯撒密码

print('运行该程序后,将加密保存在新文件')
#打旧文件
file=open("test.txt",'r')
f=file.read()
code=''

for i in f:
    code=code+chr(ord(i)+3)
#打开新文件
file = open("new_test.txt", 'w')
file.write(code)
file.close()

2、词频统计

file = open("font.txt", "r")
words = []

for line in file:
    for word in line.replace('\n', '').lower().split(" "):
        words.append(word)

map = {}
for word in words:
    map[word] = map[word] + 1 if word in map.keys() else 1

for key in map:
    print(key + ": " + str(map[key]))

(三)截图展示

1、凯撒密码

原文:

密文:

 2、词频统计

font文件(歌词)

运行结果(未截全)

 四、函数定义

(一)基本要求

  • 加密函数
  • 解密函数
  • 读文本函数

(二)编码实现

def jiami():
    Str = input('请输入;')
    for line in Str:
        print('{}'.format(chr(ord(line) + 3)), end="")

def wenben():
    print('运行该程序后,将加密保存在新文件')
    # 打旧文件
    file = open("test.txt", 'r')
    f = file.read()
    code = ''

    for i in f:
        code = code + chr(ord(i) + 3)
    # 打开新文件
    file = open("new_test.txt", 'w')
    file.write(code)
    file.close()
wenben()
 

(三)截图展示

运行wenben()函数后:

 

posted @ 2019-03-10 18:14  代码搬运小生  阅读(154)  评论(0编辑  收藏  举报