字符串操作、文件操作,英文词频统计预处理
作业来源:https://edu.cnblogs.com/campus/gzcc/GZCC-16SE1/homework/2684
1.字符串操作:
- 解析身份证号:生日、性别、出生地等。
- 凯撒密码编码与解码
- 网址观察与批量生成
代码实现
①解析身份证号
def main(id):
province = id[0:2]
city = id[2:4]
county = id[4:6]
year = id[6:10]
month = id[10:12]
day = id[13:14]
SN = id[-4:-1]
if int(id[-2]) %2 != 2:
sex = '男'
else:
sex = '女'
check = id[-1]
print("地址为:{}省,{}市,{}县,生日为:{}年{}月{}日,顺序号为{},性别为{},校验码为{}".format(province,city,county,year,month,day,SN,sex,check))
id = input("请输入身份证号码:")
main(id)
②凯撒密码编码与解码
def encoding(code):
print("加密前:{}".format(code))
print("加密后:",end="")
for x in range(len(code)):
print(chr(ord(code[x]) + 5), end='')
def decoding(code):
print("解密前:{}".format(code))
print("解密后:",end="")
for x in range(len(code)):
print(chr(ord(code[x]) - 5), end='')
code = input("请输入要加密的编码:")
way = int(input("请输入要运行的方法(1为加密,2为解密)"))
if (way == 1):
encoding(code)
if (way == 2):
decoding(code)
③网址观察与批量生成
def WebpageGeneration(url):
for i in range(10):
print(url+"{}.html".format(i+1))
url="https://i.cnblogs.com/EditPosts.aspx?opt=1"
WebpageGeneration(url)
效果截图

2.英文词频统计预处理
- 下载一首英文的歌词或文章或小说。
- 将所有大写转换为小写
- 将所有其他做分隔符(,.?!)替换为空格
- 分隔出一个一个的单词
- 并统计单词出现的次数。
代码实现
text = '''Happy Birthday to you
Happy Birthday to you
Happy Birthday to you
Happy Birthday to you
'''
Punctuation = "',.?!()"
for i in Punctuation:
song = text.replace(i, ' ')
word_frequency = {}
song = text.lower()
song = text.split()
for i in song:
if i not in word_frequency:
word_frequency[i] = 1
else:
word_frequency[i] +=



3.文件操作
- 同一目录、绝对路径、相对路径
- 凯撒密码:从文件读入密函,进行加密或解密,保存到文件。
- 词频统计:下载一首英文的歌词或文章或小说,保存为utf8文件。从文件读入文本进行处理。
课上练习前面的操作,后面两个的都没了解。等明白了再补回来。
4.函数定义
- 加密函数
- 解密函数
- 读文本函数

浙公网安备 33010602011771号