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

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

 

1.字符串操作

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

 1 # -*- coding:utf-8 -*-
 2 idCard=input("输入身份证号码:")
 3 local=idCard[0:2]
 4 year= idCard[6:10]
 5 month= idCard[10:12]
 6 day= idCard[12:14]
 7 sex=idCard[16:17]
 8 address={'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':'澳门特别行政区'}
 9 print("地区:" + address.get(local),"生日:{}年{}月{}日".format(year, month, day))
10 if(int(sex)%2==0):
11     print("性别:女")
12 else:
13     print("性别:男")

 

运行结果:

  

  凯撒密码编码与解码

 1 def decode():
 2     code = input("请输入原码:\n")
 3     decode = ""
 4     for n in code:
 5         if -128 >= ord(n) or 127 <= ord(n):
 6             print("原码取值超出范围!!!")
 7             break
 8         decode += chr(ord(n) + 3)
 9 
10     print("原码:" + code + "\n")
11     print("编码:" + decode + "\n")
12     return
13 
14 
15 def encode():
16     decode = input("请输入编码:\n")
17     encode = ""
18     for n in decode:
19         if -125 >= ord(n) or 130 <= ord(n):
20             print("编码取值超出范围!!!")
21             break
22         encode += chr(ord(n) - 3)
23 
24     print("编码:" + decode + "\n")
25     print("译码:" + encode + "\n")
26     return
27 
28 
29 while 1:
30     choose = input("请选择功能:\n 1、编码\n 2、译码\n")
31     if choose == "1":
32         decode()
33     elif choose == "2":
34         encode()
35     else:
36         print("\n\n请重新输入:\n\n")

 

运行结果:

 

网址观察与批量生成

1 for i in range(2,30):
2     url='https://edu.cnblogs.com/homework?page={}'.format(i)
3     print(url)

运行结果:

 

 

2.英文词频统计

 1 import re
 2 
 3 file = open("two tigers.txt", "r+")
 4 str = file.read()
 5 print("读取的字符串是 : \n", str)
 6 
 7 str = re.sub('[\r\n\t,]', ' ', str)
 8 words = str.split(" ")
 9 
10 single = []
11 
12 for word in words:
13     if (word not in single):
14         single.append(word)
15 
16 i = 1
17 while single:
18     print(single[0], "出现次数:", words.count(single[0]), end="     ")
19     single.pop(0)
20 
21     if i % 4 == 0:
22         print("\n")
23 
24     i += 1
25 
26 file.close()

 

运行结果:

posted @ 2019-03-06 20:29  qwertuyt124  阅读(135)  评论(0编辑  收藏  举报