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

1.字符串操作:

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

代码

def get_id_code(id):
    province = id[0:2]
    city = id[2:4]
    county = id[4:6]
    year = id[6:10]
    month = id[10:12]
    day = id[-6:-4]
    SN = id[-4:-1]
    if int(id[-2]) %2 != 1:
        sex = ''
    else:
        sex = ''
    check = id[-1]
    print("地址为:{}省,{}市,{}县,生日为:{}年{}月{}日,顺序号为{},性别为{},校验码为{}".format(province,city,county,year,month,day,SN,sex,check))
id = input("请输入身份证号码:")
get_id_code(id)

运行结果

凯撒密码编码与解码

代码

def encoding(code):
print("加密前:{}".format(code))
print("加密后:",end="")
for x in range(len(code)):
print(chr(ord(code[x]) + 3), end='')
def decoding(code):
print("解密前:{}".format(code))
print("解密后:",end="")
for x in range(len(code)):
print(chr(ord(code[x]) - 3), end='')
code = input("请输入要加密的编码:")
way = int(input("请输入要运行的方法(1为加密,2为解密)"))
if (way == 1):
encoding(code)
if (way == 2):
decoding(code)

运行结果

网址观察与批量生成

代码

def site_observation(url):
    for i in range(1,10):
        print(url + '{}.html'.format(i))
site_observation('http://news.gzcc.cn/html/xiaoyuanxinwen/')

运行结果

2.英文词频统计预处理

  • 下载一首英文的歌词或文章或小说。
  • 将所有大写转换为小写
  • 将所有其他做分隔符(,.?!)替换为空格
  • 分隔出一个一个的单词
  • 并统计单词出现的次数。

代码

import string
text=''' Remembering me,
  Discover and see
  All over the world,
  She's known as a girl
  To those who a free,
  The mind shall be key
  Forgotten as the past
  'Cause history will last
  God is a girl,
  Wherever you are,
  Do you believe it, can you recieve it?
  God is a girl,
  Whatever you say,
  Do you believe it, can you recieve it?
  God is a girl,
  However you live,
  Do you believe it, can you recieve it?
  God is a girl,
  She's only a girl,
  Do you believe it, can you recieve it?
  She wants to shine,
  Forever in time,
  She is so driven, she's always mine
  Cleanly and free,
  She wants you to be
  A part of the future,
  A girl like me
  There is a sky,
  Illuminating us, someone is out there
  That we truly trust
  There is a rainbow for you and me
  A beautiful sunrise eternally
  God is a girl
  Wherever you are,
  Do you believe it, can you recieve it?
  God is a girl
  Whatever you say,
  Do you believe it, can you recieve it?
  God is a girl
  However you live,
  Do you believe it, can you recieve it?
  God is a girl
  She's only a girl,
  Do you believe it, can you recieve it?
'''
for c in string.punctuation:
text = text.replace(c," ")
text = text.lower()
text = text.split()
count = {}
for i in text:
if i not in count:
count[i] = 1
else:
count[i] +=1
print(count)

运行结果

3.文件操作

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

凯撒密码

代码

def encoding():
    f1 = open("file.txt","r",encoding='utf-8')
    f2 = open("code.txt","w",encoding='utf-8')
    code = f1.read()
    print("加密:")
    for x in range(len(code)):
        print(chr(ord(code[x]) + 3),end='')
        f2.write(chr(ord(code[x]) + 3))
    f1.close()
    f2.close()
def decoding():
    f1 = open("code.txt", "r",encoding='utf-8')
    f2 = open("file.txt", "w",encoding='utf-8')
    code = f1.read()
    print("\n解密:")
    for x in range(len(code)):
        print(chr(ord(code[x])-3),end='')
        f2.write(chr(ord(code[x]) - 3))
    f1.close()
    f2.close()
encoding()
decoding()

运行结果

词频统计

代码

import string
def count():
    f = open("count.txt","r",encoding='utf-8')
    text = f.read()
    f.close()
    text = text.lower()
    for c in string.punctuation:
        text = text.replace(c," ")
    text = text.lower()
    text = text.split()
    count = {}
    for i in text:
        if i not in count:
           count[i] = 1
        else:
           count[i] +=1
    print(count)
count()

运行结果

4.函数定义

加密函数

def encoding():
    f1 = open("file.txt","r",encoding='utf-8')
    f2 = open("code.txt","w",encoding='utf-8')
    code = f1.read()
    print("加密:")
    for x in range(len(code)):
        print(chr(ord(code[x]) + 3),end='')
        f2.write(chr(ord(code[x]) + 3))
    f1.close()
    f2.close()

解密函数

def decoding():
    f1 = open("code.txt", "r",encoding='utf-8')
    f2 = open("file.txt", "w",encoding='utf-8')
    code = f1.read()
    print("\n解密:")
    for x in range(len(code)):
        print(chr(ord(code[x])-3),end='')
        f2.write(chr(ord(code[x]) - 3))
    f1.close()
    f2.close()

读文本函数

def count():
    f = open("count.txt","r",encoding='utf-8')
    text = f.read()
    f.close()
    text = text.lower()
    for c in string.punctuation:
        text = text.replace(c," ")
    text = text.lower()
    text = text.split()
    count = {}
    for i in text:
        if i not in count:
           count[i] = 1
        else:
           count[i] +=1
    print(count)

转载于:https://www.cnblogs.com/Evariste-Galois/p/10505416.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值