1.字符串操作:

  • 解析身份证号:生日、性别、出生地等。
  • 凯撒密码编码与解码
  • 网址观察与批量生成

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

def function3():
print('请输入身份证号')
ID = input()
if len(ID) != 18:
print('请输入有效的身份证号码')
else:
print('身份证号码格式正确')
birth = ID[6:14]
print('您的生日是:', format(birth))
check = ID[14:17]
if int(check) % 2 == 0:
print('您的性别为:女')
else:
print('您的性别为:男')
adress = ID[0:6]
print('您的地址号码是:', format(adress), '可根据号码上网查')

结果截图:

凯撒密码编码与解码

def function1():
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 function2():
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 function4():
for i in range(1, 10):
url = 'http://www.baidu.com/{}.html'.format(i)
print(url)

结果截图:

2.英文词频统计预处理

  • 下载一首英文的歌词或文章或小说。
  • 将所有大写转换为小写
  • 将所有其他做分隔符(,.?!)替换为空格
  • 分隔出一个一个的单词
  • 并统计单词出现的次数。
def function5():
str1="""I'm a big big girl !
In a big big world !
It's not a big big thing if you leave me.
But I do do feel.
That I too too will miss you much.
Miss you much !
I can see the first leaf falling.
It's all yellow and nice.
It's so very cold outside.
Like the way I'm feeling inside.
I'm a big big girl !
In a big big world !
It's not a big big thing if you leave me.
But I do do feel.
That I too too will miss you much.
Miss you much !
Outside it's now raining.
And tears are falling from my eyes.
Why did it have to happen ?
Why did it all have to end ?
I'm a big big girl !
In a big big world !
It's not a big big thing if you leave me.
But I do do feel.
That I too too will miss you much.
Miss you much !
I have your arms around me ooooh like fire.
But when I open my eyes.
You're gone !
I'm a big big girl !
In a big big world !
It's not a big big thing if you leave me.
But I do do feel.
That I too too will miss you much.
Miss you much !
I'm a big big girl !
In a big big world !
It's not a big big thing if you leave me.
But I do feel that will miss you much !
Miss you so much !"""
#字符
s1 = str1.lower()
print(s1)
# 去掉空格
str1 = str1.lstrip()
print(str1)
# 将歌词的每个单词分隔组成列表形式
print("将歌词的每个单词分隔组成列表形式:")
strList = str1.split()
print(strList)
# 计算单词出现的次数
print("计算单词出现的次数:")
strSet = set(strList)
for word in strSet:
print(word, strList.count(word))

结果截图:

3.文件操作

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

凯撒密码

def function1():
print("从文本读取到的密文是:") fo = open(r"..\bd\mypassword", "r",encoding="utf-8")
str1 = fo.read()
# 文本读取到的密码
print(str1)
fo.close()
str_raw = str1 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))
print("写入文本中,请稍后。。。")
fo = open(r"..\bd\mypassword", "w")
fo.write(str_list_encry)
fo.close() def function2():
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 == "":
function1()
elif choice == "":
function2() else:
print (u"您的输入有误!")

词频统计

import os
path =os.getcwd()
fo=open(r"..\python1\what","r")
str1=fo.read()
#print(fo.readline())
fo.close()
s1=str1.lower()
#print(s1)
#去掉空格
str1=str1.lstrip()
#print(str1)
#将歌词的每个单词分隔组成列表形式
print("将歌词的每个单词分隔组成列表形式:")
strList=str1.split()
print(strList)
#计算单词出现的次数
print("计算单词出现的次数:")
strSet=set(strList)
for word in strSet:
print(word,strList.count(word))

python字符串操作、文件操作,英文词频统计预处理的更多相关文章

  1. 1.字符串操作:& 2.英文词频统计预处理

    1.字符串操作: 解析身份证号:生日.性别.出生地等. ID = input('请输入十八位身份证号码: ') if len(ID) == 18: print("你的身份证号码是 " ...

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

    一.字符串操作: 解析身份证号:生日.性别.出生地等. 凯撒密码编码与解码 网址观察与批量生成 2.凯撒密码编码与解码 凯撒加密法的替换方法是通过排列明文和密文字母表,密文字母表示通过将明文字母表向左 ...

  3. python基础之 列表、元组操作 字符串操作 字典操作 集合操作 文件操作 字符编码与转码

    本节内容 列表.元组操作 字符串操作 字典操作 集合操作 文件操作 字符编码与转码 1. 列表.元组操作 列表是我们最以后最常用的数据类型之一,通过列表可以对数据实现最方便的存储.修改等操作 定义列表 ...

  4. 组合数据类型,英文词频统计 python

    练习: 总结列表,元组,字典,集合的联系与区别.列表,元组,字典,集合的遍历. 区别: 一.列表:列表给大家的印象是索引,有了索引就是有序,想要存储有序的项目,用列表是再好不过的选择了.在python ...

  5. Python的高级文件操作(shutil模块)

    Python的高级文件操作(shutil模块) 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 如果让我们用python的文件处理来进行文件拷贝,想必很多小伙伴的思路是:使用打开2个 ...

  6. Python入门篇-文件操作

    Python入门篇-文件操作 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.文件IO常用操作 open:打开 read:读取 write:写入 close:关闭 readlin ...

  7. python os&shutil 文件操作

    python os&shutil 文件操作 # os 模块 os.sep 可以取代操作系统特定的路径分隔符.windows下为 '\\' os.name 字符串指示你正在使用的平台.比如对于W ...

  8. 跟着ALEX 学python day3集合 文件操作 函数和函数式编程 内置函数

    声明 : 文档内容学习于 http://www.cnblogs.com/xiaozhiqi/  一. 集合 集合是一个无序的,不重复的数据组合,主要作用如下 1.去重 把一个列表变成集合 ,就自动去重 ...

  9. python基础篇(文件操作)

    Python基础篇(文件操作) 一.初始文件操作 使用python来读写文件是非常简单的操作. 我们使用open()函数来打开一个文件, 获取到文件句柄. 然后通过文件句柄就可以进行各种各样的操作了. ...

随机推荐

  1. hadoop--hive数据仓库

    一.hive概述 Hive是基于 Hadoop 的一个[数据仓库工具],可以将结构化的数据文件映射为一张数据库表,并提供简单的 sql 查询功能,可以将 sql 语句转换为 MapReduce 任务进 ...

  2. Modelsim SE自动化仿真——如何将.do文件中自定义的库链接到testbench顶层模块

    我们用Modelsim SE进行仿真时,为了方便,一般会编写.do文件来启动当前仿真.关于.do文件的编写,一般网上都有成型的模板,我们只要稍微改几个参数,就可以符合我们的仿真需求了.但是如果仿真时需 ...

  3. sqlserver分区视图中分区列的规则

    分区列规则 分区列存在于每个成员表上,并且通过 CHECK 约束标识特定表中的可用数据.分区列必须遵守如下规则: 每个基表都拥有键值由 CHECK 约束所强制的分区列.每个表的 CHECK 约束的键范 ...

  4. 前端- css - 总结

    1.css层叠样式表 1.什么是CSS? CSS是指层叠样式表(Cascading Style Sheets),样式定义如何显示HTML元素,样式通常又会存在于样式表中. 也就是说把HTML元素的样式 ...

  5. Unity3d之Hash&Slash学习笔记(一)--角色属性类的构架

    角色属性类的构架 角色属性类有8个类,继承关系如下图: 每个类的具体作用见之后的随笔

  6. Object C学习笔记4-内存管理

    Object-C的内存管理和.NET有些不一样,.NET的内存回收机制是使用GC自动处理回收,而Object-C本质上还是C语言,所以很多时候还是需要手动去管理内存回收. 1. Object-C生成一 ...

  7. 16节实用性爆棚的Ps课:零基础秒上手,让你省钱也赚钱

    ps视频教程,ps自学视频教程.ps免费视频教程下载,16节实用性爆棚的Ps课教程视频内容较大,分为俩部分: 16节实用性爆棚的Ps课第一部分:百度网盘,https://pan.baidu.com/s ...

  8. 初学者下载使用Python遇到的问题看它就行了

    首先在python管网(www.python.org)中找到对应的版本与系统,以(window7系统64位python3.7.3为例) 打开电脑--打开浏览器--输入www.python.org--d ...

  9. TPO-21 C2 Which elective courses to take

    /* 加粗:语音部分 * 红色:单词部分 * 斜体:语法部分 * 下划线:信号词/句 */ 第 1 段 1.Listen to a conversation between a student and ...

  10. 宝塔中mysql数据库命名小坑

    今天在通过宝塔新建网站,添加mysql数据库,名字中间有下划线,发现能够创建成功,但是实际链接后,是没有这个数据库的.是宝塔的原因还是liunx服务器的原因? 不支持下划线的数据库名字吗? 比如 bo ...