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

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

1.字符串操作:

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

2.英文词频统计预处理

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

3.文件操作

  • 同一目录、绝对路径、相对路径
  • 凯撒密码:从文件读入密函,进行加密或解密,保存到文件。
  • 词频统计:下载一首英文的歌词或文章或小说,保存为utf8文件。从文件读入文本进行处理。

 4.函数定义

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

  • 解析身份证号:生日、性别、出生地

         核心代码:

ID = input('请输入18位身份证号码: ')
if len(ID) == 18:
print("你的身份证号码是 " + ID)
else:
print("错误的身份证号码")

dizhi = ID[0:6]
shengri = ID[6:14]
sex = ID[16]
ID_check = ID[17]

year = shengri[0:4]
moon = shengri[4:6]
day = shengri[6:8]

if int(sex)%2 == 0:
xingbie = "女士"
else:
xingbie = "先生"

print(xingbie+"你好,你的生日为: "+year+''+moon+''+day+''+"你的出生地区编号为:"+dizhi)

结果截图如下:

  • 词频统计:下载一首英文的歌词或文章或小说,保存为utf8文件。从文件读入文本进行处理。

          核心代码:


f = open("text.txt","r",encoding='utf-8')
text = f.read()
f.close()
text.replace(" ", "")
text.replace(",", "")
text.replace(".", "")
text=text.split()
count = {}
for i in text:
if i not in count:
count[i] = 1
else:
count[i] += 1
print(count)
 

           结果截图如下:

          text.txt内容如下:

  • 网址观察与批量生成

          核心代码:

url="https://www.bilibili.com/video/av"
for i in range(1, 10):
    print(url + str(i))

         结果截图如下:

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

           核心代码如下:

def code(i):
    result = ""
    for a in i:
        if ord(a) > 119:
            result = result + chr(ord(a) - 23)
        else:
            result = result + chr(ord(a) + 3)
    return result


def decode(i):
    result = ""
    for a in i:
        if ord(a) < 100:
            result = result + chr(ord(a) + 23)
        else:
            result = result + chr(ord(a) - 3)
    return result

f1 = open("code.txt","r",encoding='utf-8')
i_before = f1.read()
f1.close()
result_code=code(i_before)
print("加密后的信息为:"+result_code)
f2 = open("sceret.txt","w",encoding='utf-8')
f2.write(result_code)
f2.close()

f2 = open("sceret.txt","r",encoding='utf-8')
i_after = f2.read()
f2.close()
result_decode=decode(i_after)
print("加密前的信息为:"+result_decode)

              结果截图如下:

code.txt:

把code中的内容进行凯撒加密,加密后的信息保存到密件sceret.txt

        

原文地址:https://www.cnblogs.com/lqscmz/p/10506801.html