【大数据应用技术】作业二 |字符串操作,英文词频统计预处理

作业的要求来自于:https://edu.cnblogs.com/campus/gzcc/GZCC-16SE2/homework/2646

1.字符串操作

(1)解析身份证号:生日、性别、出生地等

 ① 代码

 1 # -*- coding: utf-8 -*-
 2 
 3 ID=input('请输入18位省份证号码:');
 4 if len(ID) == 18:
 5     print("你输入的身份证号码为"+ID);
 6 else:
 7     print("你输入的身份证号码有误!");
 8 
 9 ID_add = ID[0:6]; #ID_add 指的是身份证的区域代码
10 ID_birth = ID[6:14]; #ID_birth 指的是出生年月
11 ID_sex = ID[14:17]; #ID_sex 指的是性别
12 
13 #出生年月
14 year = ID_birth[0:4];
15 month = ID_birth[4:6];
16 day = ID_birth[6:8];
17 print("你的出生日期为:"+year+""+month+""+day+"");
18 
19 #性别
20 if int(ID_sex)%2 == 0:
21     print("性别:女");
22 else:
23     print("性别:男");

② 运行结果

 

(2)凯撒密码编码与解码

 ① 代码

 1 # -*- coding: utf-8 -*-
 2 import os
 3 
 4 alphabet=['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'];
 5 #1.编码
 6 def get_code():
 7     print('请输入要编码的字符:');
 8     s=input();
 9     str1='';
10     for i in range(len(s)):
11         if s[i] in alphabet:
12             #temp=ord(s[i]);
13             #num=(temp-97+3)%26;
14             #str1=chr(num+97);
15             str1 = alphabet[(ord(s[i])-97+3)%26];
16             print(str1,end="");
17         else:
18             print(" ",end="");
19 
20 #2.解码
21 def decode():
22     print('请输入要解码的字符:');
23     s = input();
24     str2 = '';
25     for i in range(len(s)):
26         if s[i] in alphabet:
27             str2 = alphabet[(ord(s[i])-97-3)%26];
28             print(str2, end="");
29         else:
30             print(" ", end="");
31 
32 while True:
33     print("\n");
34     print('1.编码');
35     print('2.解码');
36     choice = input("请输入1或者2进行选择:");
37     if choice == '1':
38         get_code();
39     elif choice == '2':
40         decode();
41     else:
42         print('输入错误!');

② 运行结果

 

 

(3)网址观察与批量生成

 ① 代码

1 # -*- coding: utf-8 -*-
2 
3 for i in range(2,6):
4         url='http://xybgs.gzcc.cn/html/xuxigg/{}.html'.format(i);
5         print(url);

② 运行结果

 

2.英文词频统计预处理

① 英文文章

在这里我将文章放在了跟源代码文件同级的路径上

② 源代码

 1 f=open('test.txt','r',encoding='utf8');
 2 article=f.read();
 3 f.close();
 4 
 5 article = article.lower();
 6 sep = ".-,:";
 7 for s in sep:
 8     article=article.replace(s,' ');
 9 print(article.split(' '));
10 print(article.count('friend'));

③ 运行结果

posted @ 2019-03-04 10:23  Molly134  阅读(285)  评论(0编辑  收藏  举报