博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
字符串操作、文件操作,英文词频统计预处理
阅读量:4680 次
发布时间:2019-06-09

本文共 3918 字,大约阅读时间需要 13 分钟。

本作业要求来自于:

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()

转载于:https://www.cnblogs.com/lingzihui/p/10510533.html

你可能感兴趣的文章
面试题之求二叉树的深度
查看>>
struts2---访问WEB
查看>>
jQuery学习(三)
查看>>
[VJ]输出m/n,若是循环体只输出第一节
查看>>
【问题】background:url(imagePath)不能显示图片
查看>>
linux 安装mysql
查看>>
团队项目冲刺第五天
查看>>
Dubbo-Fail to decode request due to: RpcInvocation
查看>>
android笔记5——同一个Activity中Fragment的切换
查看>>
Pillow & OpenCV安装
查看>>
【3dsmax2016】安装图文教程、破解注册以及切换语言方法
查看>>
markdown
查看>>
WebService-01-使用jdk发布第一个WebService服务并调用
查看>>
mysql 关键字于数据库字段于关键字冲突的问题
查看>>
【bzoj2694】Lcm 莫比乌斯反演+线性筛
查看>>
【bzoj3110】[Zjoi2013]K大数查询 整体二分+树状数组区间修改
查看>>
Django表查询补充
查看>>
阅读计划
查看>>
Windows Live Writer 代码插件测试
查看>>
边工作边刷题:70天一遍leetcode: day 32-1
查看>>