字符串、文件操作,英文词率统计预处理
该作业要求来于:https://edu.cnblogs.com/campus/gzcc/GZCC-16SE1/homework/2684
1.字符串操作:
- 解析身份证号:生日、性别、出生地等。
- 凯撒密码编码与解码
- 网址观察与批量生成
2.英文词频统计预处理
- 下载一首英文的歌词或文章或小说。
- 将所有大写转换为小写
- 将所有其他做分隔符(,.?!)替换为空格
- 分隔出一个一个的单词
- 并统计单词出现的次数。
3.文件操作
- 同一目录、绝对路径、相对路径
- 凯撒密码:从文件读入密函,进行加密或解密,保存到文件。
- 词频统计:下载一首英文的歌词或文章或小说,保存为utf8文件。从文件读入文本进行处理。
4.函数定义
- 加密函数
- 解密函数
- 读文本函数
作业详情:
ID = input('请输入十八位身份证号码: ')
if len(ID) == 18:
print("你的身份证号码是 " + ID)
else:
print("错误的身份证号码")
ID_add = ID[0:6]
ID_birth = ID[6:14]
ID_sex = ID[14:17]
ID_check = ID[17]
year = ID_birth[0:4]
moon = ID_birth[4:6]
day = ID_birth[6:8]
print("生日: " + year + '年' + moon + '月' + day + '日')
if int(ID_sex) % 2 == 0:
print('性别:女')
else:
print('性别:男')
W = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2]
ID_num = [18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2]
ID_CHECK = ['1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2']
ID_aXw = 0
for i in range(len(W)):
ID_aXw = ID_aXw + int(ID[i]) * W[i]
ID_Check = ID_aXw
if ID_check == ID_CHECK[ID_Check]:
print('正确的身份证号码')
else:
print('错误的身份证号码')
fo = open("D:\Study\My heart will go on.txt", "r", encoding='utf-8')
song = fo.read()
s = ",.?!"
for i in s:
song.replace(i,"")
f = song.lower().split()
for i in f:
print(i+":"+str(f.count(i)))
every:1
night:1
in:5
my:9
dreams:1
i:8
see:1
you,i:1
feel:1
you:10
that:4
is:1
how:1
i:8
know:2
you:10
go:10
on:12
far:3
across:1
the:5
distance:1
and:12
spaces:1
between:1
us:2
you:10
have:1
come:1
to:2
show:1
you:10
go:10
on:12
near:2
far:3
wherever:2
you:10
are:3
i:8
believe:2
that:4
the:5
heart:9
does:2
go:10
on:12
once:2
more:2
you:10
open:2
the:5
door:2
and:12
you're:3
here:3
in:5
my:9
heart:9
and:12
my:9
heart:9
will:4
go:10
on:12
and:12
on:12
love:2
can:1
touch:1
us:2
one:2
time:2
and:12
last:1
for:1
a:1
lifetime:1
and:12
never:1
let:1
go:10
till:1
we're:1
gone:1
love:2
was:1
when:1
i:8
loved:1
you:10
one:2
true:1
time:2
i:8
hold:1
to:2
in:5
my:9
life:1
well:1
always:1
go:10
on:12
near:2
far:3
wherever:2
you:10
are:3
i:8
believe:2
that:4
the:5
heart:9
does:2
go:10
on:12
once:2
more:2
you:10
open:2
the:5
door:2
and:12
you're:3
here:3
in:5
my:9
heart:9
and:12
my:9
heart:9
will:4
go:10
on:12
and:12
on:12
you're:3
here:3
there's:1
nothing:1
i:8
fear:1
and:12
i:8
know:2
that:4
my:9
heart:9
will:4
go:10
on:12
we'll:1
stay:1
forever:1
this:1
way:1
you:10
are:3
safe:1
in:5
my:9
heart:9
and:12
my:9
heart:9
will:4
go:10
on:12
and:12
on:12
3.凯撒密码
def encryption():
str_raw = input("请输入明文:")
k = int(input("请输入位移值:"))
str_change = str_raw.lower()
str_list = list(str_change)
str_list_encry = str_list
i = 0
while i < len(str_list):
if ord(str_list[i]) < 123-k:
str_list_encry[i] = chr(ord(str_list[i]) + k)
else:
str_list_encry[i] = chr(ord(str_list[i]) + k - 26)
i = i+1
print ("加密结果为:"+"".join(str_list_encry))
def decryption():
str_raw = input("请输入密文:")
k = int(input("请输入位移值:"))
str_change = str_raw.lower()
str_list = list(str_change)
str_list_decry = str_list
i = 0
while i < len(str_list):
if ord(str_list[i]) >= 97+k:
str_list_decry[i] = chr(ord(str_list[i]) - k)
else:
str_list_decry[i] = chr(ord(str_list[i]) + 26 - k)
i = i+1
print ("解密结果为:"+"".join(str_list_decry))
while True:
print (u"1. 加密")
print (u"2. 解密")
choice = input("请选择:")
if choice == "1":
encryption()
elif choice == "2":
decryption()
else:
print (u"您的输入有误!")
网址观察与批量生成
for i in range(0,6):
url='http://news.gzcc.cn/html/xiaoyuanxinwen/1.html'.format(i)
print(url)
3.文件操作
- 同一目录、绝对路径、相对路径
- 凯撒密码:从文件读入密函,进行加密或解密,保存到文件。
-
file=open('Read.txt','r') before=file.read(); result=" " for i in before: result += chr(ord(i) + 3) file=open(r'D:\Study\Write.txt','w') file.write(result) file.close()
-
4.函数定义 加密函数 解密函数 读文本函数