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

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

    1.字符串操作:

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

    # 获取身份证号中的出生地与性别
    identy = input("请输入您的身份证号:")
    while (len(identy)!=18):
        print("您的身份证号码输入错误")
        identy = input("请重新输入您的身份证号:")
    birthplace = input("请输入你的出生地:")
    print("你的出生地是{}".format(birthplace))
    
    sex = identy[-2]
    if int(sex) % 2 == 0:
        print("性别为女")
    else:
        print("性别为男")

    凯撒密码编码与解码

    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"您的输入有误!")

    网址观察与批量生成

    import webbrowser as web
    url = 'http://news.gzcc.cn/html/xiaoyuanxinwen/'
    web.open_new_tab(url)
    for i in range(2, 4):
        web.open_new_tab('http://news.gzcc.cn/html/xiaoyuanxinwen/'+str(i)+'.html')
    for i in range(2, 10):
       url = 'http://news.gzcc.cn/html/xiaoyuanxinwen/{}.html'.format(i)
       print (url)

    2.英文词频统计预处理

     下载一首英文的歌词或文章或小说。

     将所有大写转换为小写

     将所有其他做分隔符(,.?!)替换为空格

     分隔出一个一个的单词

     并统计单词出现的次数

    #获取单词函数定义
    def getTxt():
        txt = open('English.txt').read()
        txt = txt.lower()
        for ch in '!"@#$%^&*()+,-./:;<=>?@[\]_`~{|}': #替换特殊字符
            txt.replace(ch, ' ')
        return txt
    #1.获取单词
    EnglishTxt = getTxt()
    
    #2.大写字母转换成小写字母
    exchange = EnglishTxt.lower()
    print(exchange)
    
    #3.切割为列表格式
    txtArr = EnglishTxt.split()
    
    #4.遍历统计
    counts = {}
    for word in txtArr:
        counts[word] = counts.get(word, 0) + 1
    
    #5.转换格式,方便打印,将字典转换为列表
    countsList = list(counts.items())
    countsList.sort(key=lambda x:x[1], reverse=True)#按次数从大到小排序
    
    #6.打印
    for i in range(10):
        word, count = countsList[i]
        print('{0:<10}{1:>5}'.format(word,count))

    3.文件操作

    同一目录、绝对路径、相对路径

    #相对路径
    open('English.txt')  
    #相对路径
    open('/untitled/English.txt')  
    #绝对路径
    open('C:UsersAdministratorPycharmProjectsuntitledEnglish.txt')  

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

    file=open("mima.txt",'r')
    text=file.read()
    file.close
    miwen = ''
    for i in text:
      miwen += chr(ord(i)+6)
    print("文章加密后为:",miwen)
    file=open("mima.txt",'w')
    file.write(miwen)
    file.close()

    词频统计:下载一首英文的歌词或文章或小说,保存为utf8文件。从文件读入文本进行处理。

    # coding=utf-8
    file = open("English.txt")
    text = file.readline()
    file.close()
    s = ",.?!"
    for i in s:
        text = text.replace(i, " ")
    text1 =text.lower().split()
    
    count = {}
    for j in text1:
        try:
            count[j] = count[j] + 1
        except KeyError:
            count[j] = 1
    print(count)

    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 read() :
          f = open('C:UsersAdministratorPycharmProjectsuntitled.English.txt','r')
          # data = f.read(10000)
          # print(data)
  • 相关阅读:
    3.流程控制语句
    pandas数据处理(一)pymongo数据库量大插入时去重速度慢
    windows下载安装redis
    Scrapy(一)爬知乎所有用户信息
    Mongodb去除重复的数据,pymongo去重
    selenium爬虫报错:Message: stale element reference: element is not attached to the page document 促成1分钟爬完斗鱼主播信息。
    用pyspider爬斗鱼主播信息
    曼城新闻情报站(二)Django框架的爬虫
    曼城新闻情报站(一)爬取3大网站的曼城新闻
    Beautiful Soup多线程爬取斗鱼所有主播信息(改良版)
  • 原文地址:https://www.cnblogs.com/zjby/p/10508325.html
Copyright © 2011-2022 走看看