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

1.字符串操作:

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

# -*- coding: UTF-8 -*-
id = input("请输入你的身份证号码:")
print("你的身份证号码为:", id)
brithyear = id[6:10]
brithmoth = id[10:12]
brithday = id[12:14]
print("你的出生日期为:", brithyear+"年"+brithmoth+"月"+brithday+"日")
sex=id[-2];
if int(sex) % 2 == 0:
print("性别为女")
else:
print("性别为男")

凯撒密码编码与解码

# -*- coding: UTF-8 -*-

text = input("请输入明文:")
choose = input("1.编码 2.译码\n")
if int(choose) == 1:
for i in text:
s = ord(i)+3
print(chr(s), end="")
if int(choose) == 2:
for i in text:
s = ord(i)-3
print(chr(s), end="")

网址观察与批量生成

# -*- coding: UTF-8 -*-
for i in range(2,10):
url = 'http://news.gzcc.cn/html/xiaoyuanxinwen/{}.html'.format(i)
print(url)

2.英文词频统计预处理
f = open(r'english.txt', 'r')
text = f.read()
print(text)
f.close()
lowerText=text.lower()
sep = ',?/!-:_'
for s in sep:
text = text.replace(s, ' ')
print(lowerText.split())
print()
print(text.count('phone'))

 


posted on 2019-03-06 20:35  朱志杰  阅读(210)  评论(0编辑  收藏  举报