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

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

1.字符串操作:

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

凯撒密码编码与解码

网址观察与批量生成

2.英文词频统计预处理

3.文件操作

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

代码

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

add = ID[0:6]
days = ID[6:14]
sex = ID[16]
ID_check = ID[17]
day = days[6:8]
moon = days[4:6]
years = days[0:4]

if int(sex)%2 == 0:
   gender = "女士"
else:
   gender = "男士"

print(gender+"您好,您生日为: "+years+''+moon+''+day+''+"你的出生地区编号为:"+add)

结果

  统计词语频率

这次我用的是 you are beautiful这首歌的歌词来进行检索,但是不知道为什么,大小写没有全部变成小写。

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

 

网址观察与批量生成

url="https://www.http://www.acfun.cn/v/ac"
for i in range(6, 28):
    print(url + str(i))

 

.文件操作

  • 同一目录、绝对路径、相对路径
  • 凯撒密码:从文件读入密函,进行加密或解密,保存到文件。
    def code(i):
        res = ""
        for a in i:
            if ord(a) > 119:
                res = res + chr(ord(a) - 23)
            else:
                res = res + chr(ord(a) + 3)
        return res
    
    def decode(i):
        res = ""
        for a in i:
            if ord(a) < 100:
                res = res + chr(ord(a) + 23)
            else:
                res = res + chr(ord(a) - 3)
        return res
    
    f1 = open("key1","r",encoding='utf-8')
    i_before = f1.read()
    
    res_code=code(i_before)
    print("加密后:"+res_code)
    f2 = open("key11","w",encoding='utf-8')
    f2.write(res_code)
    f1.close()
    f2.close()
    f2 = open("key11","r",encoding='utf-8')
    i_after = f2.read()
    f2.close()
    res_decode=decode(i_after)
    print("加密前:"+res_decode)
    

原文地址:https://www.cnblogs.com/Winslow-liujie/p/10512450.html