作业来源:https://edu.cnblogs.com/campus/gzcc/GZCC-16SE1/homework/2684

1.字符串操作:

  • 解析身份证号:生日、性别、出生地等。
def idption():
str_id= input("请输入身份证号:")
if(len(str_id)!=):
print("你输入的身份证号有误,请重新输入")
idption()
else:
print("你出生省份为:"+str_id[:])
print("你出生市区为:" + str_id[:])
print("你出生县区为:" + str_id[:])
print("你出生日期为:" + str_id[:])
print("你出生户口派出所为:" + str_id[:])
if(str_id[:]==''):
print("你为男性且编码:" + str_id[:])
else:
print("你为女性且编码:" + str_id[:])
print("校验码为:" + str_id[:]+"\n")

运行结果:

  • 凯撒密码编码与解码
def encryption():

  print("导入文件中……")

  fo = open(r"..\Lin\file_text\Plaintext", "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 =
while i < len(str_list):
if ord(str_list[i]) < - k:
str_list_encry[i] = chr(ord(str_list[i]) + k)
else:
str_list_encry[i] = chr(ord(str_list[i]) + k - )
i = i +
print("加密结果为:" + "".join(str_list_encry))
print("保存密文……")
te="".join(str_list_encry)
fo = open(r"..\Lin\file_text\Ciphertext", "w")
fo.write(te) def decryption():
fo = open(r"..\Lin\file_text\Ciphertext", "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_decry = str_list
i =
while i < len(str_list):
if ord(str_list[i]) >= + k:
str_list_decry[i] = chr(ord(str_list[i]) - k)
else:
str_list_decry[i] = chr(ord(str_list[i]) + - k)
i = i +
print("解密结果为:" + "".join(str_list_decry))

截图:

  • 网址观察与批量生成
def webption():
for i in range(,):
url='http://news.gzcc.cn/html/xiaoyuanxinwen/{}.html'.format(i)
print(url)

截图:

2.英文词频统计预处理

  • 下载一首英文的歌词或文章或小说。
  • 将所有大写转换为小写
  • 将所有其他做分隔符(,.?!)替换为空格
  • 分隔出一个一个的单词
  • 并统计单词出现的次数。
def danciption():
text='''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 I will miss you much
miss you much...'''
s=',.!?:'
for c in s:
text=text.replace(c,'')
print(text.split())
print("单词词频次数:")
print("big:",text.count('big'))
print("Big:",text.count('Big'))
print("a:",text.count('a'))

截图如下:

3.文件操作

  • 同一目录、绝对路径、相对路径
  • 凯撒密码:从文件读入密函,进行加密或解密,保存到文件。
  • 词频统计:下载一首英文的歌词或文章或小说,保存为utf8文件。从文件读入文本进行处理。
#同一路径
f= open(r"Plaintext", 'r', encoding='utf8')
#相对路径
f2=open(r"..\Lin\file_text\Plaintext", 'r', encoding='utf8')
#绝对路径
f3=open(r"G:\PycharmProjects\Lin\file_text\Plaintext", 'r', encoding='utf8')
text=f.read()
f.close()
t=text.find(',')
print(text[0:t])
print(text[t+1:len(text)])

截图

4.函数定义

  • 加密函数
str_raw = input("请输入明文:")
k = int(input("请输入位移值:"))
str_change = str_raw.lower()
str_list = list(str_change)
str_list_encry = str_list
i =
while i < len(str_list):
if ord(str_list[i]) < -k:
str_list_encry[i] = chr(ord(str_list[i]) + k)
else:
str_list_encry[i] = chr(ord(str_list[i]) + k - )
i = i+
print ("加密结果为:"+"".join(str_list_encry))
  • 解密函数
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))

  

  • 读文本函数
f= open(r"..\Lin\file_text\Plaintext", 'r', encoding='utf8')
text=f.read()
f.close()
t=text.find(',')
print(text[:t])
print(text[t+:len(text)])

最新文章

  1. Hadoop2.2.0安装过程记录
  2. C#设计模式-状态者模式
  3. HTML &lt;div&gt; 标签
  4. Web测试中常见分享问题
  5. 说说WeakReference弱引用
  6. finereport与OA系统集成的完全方案
  7. 张艾迪(创始人):创始人故事无限N个
  8. gitlab&amp;fengoffice的ldap配置
  9. php,javscript调用百地图度API实现标记
  10. 转载:执行脚本出现bin/bash: bad interpreter: No such file or directory
  11. CPU卡中T=0通讯协议的分析与实现
  12. 反射 类的加载 Schema DOM 解析方式和解析器 命名空间
  13. windows下输入git用户名和密码错误,重新输入用户名和密码
  14. Tomcat 日志文件分割
  15. Redis protocol (redis通信协议)
  16. vs code 格式化vue代码
  17. 『计算机视觉』感受野和anchor
  18. oracle无法启动asm实例记录
  19. 【BZOJ2228】[ZJOI2011]礼物(单调栈)
  20. hdu1561 树形dp,依赖背包

热门文章

  1. 为什么从前那些.NET开发者都不写单元测试呢?
  2. .NET Core TDD 前传: 编写易于测试的代码 -- 缝
  3. 设计一个可拔插的 IOC 容器
  4. golang中Context的使用场景
  5. GlideNewDemo【Glide4.7.1版本的简单使用以及圆角功能】
  6. RequireJS入门级_RequireJS能给我们带来什么帮助?
  7. .net core中使用autofac进行IOC
  8. FSLIB.NETWORK 简易使用指南
  9. 学代码第十七天,JAVA继承
  10. 使用 phpstudy 搭建本地测试环境