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

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

1.字符串操作:
    解析身份证号:生日、性别、出生地等。

1
2
3
4
5
6
7
8
9
10
11
12
13
code=input("请输入您的身份证号:");
year=code[6:10];
month=code[10:12];
day=code[12:14];
province=code[0:2];
area={'11':'北京市','12':'天津市','13':'河北省','14':'山西省','15':'内蒙古自治区','21':'辽宁省','22':'吉林省','23':'黑龙江省','31':'上海市','32':'江苏省','33':'浙江省','34':'安徽省','35':'福建省','36':'江西省','37':'山东省','41':'河南省','42':'湖北省','43':'湖南省','44':'广东省','45':'广西壮族自治区','46':'海南省','50':'重庆市','51':'四川省','52':'贵州省','53':'云南省','54':'西藏自治区','61':'陕西省','62':'甘肃省','63':'青海省','64':'宁夏回族自治区','65':'新疆维吾尔自治区','71':'台湾省','81':'香港特别行政区','82':'澳门特别行政区'}
print("你所查询的身份证归属地为:"+area.get(province));
print("出生日期是{}年{}月{}日".format(year,month,day));
sex=code[-2];
if int(sex)%2==0:
    print("性别为女");
else:
    print("性别为男")

   

    网址观察与批量生成

1
2
for in range(2,16):
    print('http://news.gzcc.cn/html/xiaoyuanxinwen/' + str(i) + '.html')

  

    

     凯撒密码编码与解码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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"您的输入有误!")

 

 

2.英文词频统计预处理

1
2
3
4
5
6
7
8
9
10
11
12
url='''
  WHEN MINUTES BECOME HOURS
   WHEN DAYS BECOME YEARS
   AND I DON’T KNOW WHERE YOU ARE
   COLOR SEEMS SO DULL WITHOUT YOU
'''
str1=url.lower()
str2=str1.replace(",","")
str3=str2.replace("'","")
print(str3)
print(str3.split());
print(str3.count('we'))

 
3.文件操作   

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

      凯撒密码

text1=open('Tom.txt','r',encoding='utf-8')
text1=text1.read()
text11=''
for t in  text1:
    text11=text11+chr(ord(t)+3)
text2=open('Tom1.txt','a',encoding='utf-8')
text2=text2.write(text11)

     词频统计

 
text=open('gril.txt','r',encoding='utf-8')
text=text.read()
x=",?!."
for xx in x:
    text1=text.replace(xx,' ')
print(text1)
text2=open('gril1.txt','a',encoding='utf-8')
text2.write(text1)
 

 

4.函数定义

  • 加密函数
 
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))
 

 

  • 读文本函数
def readFile(filePath):
    file=open(filePath,'r',encoding='utf-8')
    return file.read()
posted on 2019-03-11 15:24  凌梓慧  阅读(173)  评论(0编辑  收藏  举报