zoukankan      html  css  js  c++  java
  • 字符串操作、文件操作,英文词频统计预处理

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

    1.字符串操作

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

      代码展示

    #身份证解析
    print("身份证解析")
    code=input("身份证号:")
    province=code[0:2]
    city=code[2:4]
    county=code[4:6]
    year=code[6:14]
    month=code[10:12]
    day=code[12:14]
    print("您的生日为:{}年{}月{}日".format(year,month,day))
    print("您的出生地为:{}省{}市{}县".format(province,city,county))

      运行效果

    • 凯撒密码编码与解码

      代码展示

    #凯撒密码
    print("凯撒加密")
    name=input("需要加密的明文:")
    mima='';
    jiemi='';
    for i in name:
        mima=mima+chr(ord(i)+3);
    print("加密后的密码:",mima)
    for i in mima:
        jiemi=jiemi+chr(ord(i)-3)
    print("解密后的明文:",name)

      运行效果

    • 网址观察与批量生成

      代码展示

    #网址批量生成
    for i in range(2,15):
        print('http://news.gzcc.cn/html/xiaoyuanxinwen/{}.html'.format(i))

      运行效果

    2.英文词频统计预处理

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

      代码展示

    #词频统计
    print("词频统计")
    file=open("lyric.txt")
    lyric=file.read();
    file.close();
    s=",.?!"
    for i in s:
        lyric=lyric.replace(i," ")
    lyric=lyric.lower().split()
    print(lyric)
    count={}
    for i in lyric:
        try:
            count[i]=count[i]+1
        except KeyError:
            count[i]=1
    print(count)

      运行效果

    3.文件操作

    • 凯撒密码:从文件读入密函,进行加密或解密,保存到文件。

      代码展示

    #凯撒密码
    print("凯撒加密")
    file=open("caesar.txt")
    caesar=file.read()
    cipher='';
    jiemi='';
    for i in caesar:
        cipher=cipher+chr(ord(i)+3);
    print("加密后的密码:",cipher)
    file=open("cipher.txt",'w')
    file.write(cipher)
    file.close()

      运行效果

    • 词频统计:

      下载一首英文的歌词或文章或小说,保存为utf8文件,从文件读入文本。

      代码展示

    #词频统计
    print("词频统计")
    file=open("lyric.txt")
    lyric=file.read();
    file.close();
    s=",.?!"
    for i in s:
        lyric=lyric.replace(i," ")
    lyric=lyric.lower().split()
    print(lyric)
    count={}
    for i in lyric:
        try:
            count[i]=count[i]+1
        except KeyError:
            count[i]=1
    print(count)

      运行效果

    4.函数定义

    • 加密函数

      代码展示

    #凯撒加密
    def caesar_cipher(plain_text):
        cipher='';
        for i in plain_text:
            cipher=cipher+chr(ord(i)+3)
        return cipher
    • 解密函数

      代码展示

    #凯撒解密
    def caesar_decrypt(cipher):
        decrypt=''
        for i in cipher:
            decrypt=decrypt+chr(ord(i)-3)
        return decrypt
    • 读文本函数

      代码展示

    #读文本函数
    def read_file(file_path):
        file=open(file_path,'r')
        return file.read();
  • 相关阅读:
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
  • 原文地址:https://www.cnblogs.com/GMUK/p/10497067.html
Copyright © 2011-2022 走看看