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

    1.字符串操作:

    • 解析身份证号:生日、性别、出生地等。
    • 凯撒密码编码与解码
    • 网址观察与批量生成
    ID = input('请输入十八位身份证号码: ')
    if len(ID) == 18:
        print("你的身份证号码是 " + ID)
    else:
        print("错误的身份证号码")
    
    ID_add = ID[0:6]
    ID_birth = ID[6:14]
    ID_sex = ID[14:17]
    ID_check = ID[17]
    
    # ID_add是身份证中的区域代码,如果有一个行政区划代码字典,就可以用获取大致地址#
    
    year = ID_birth[0:4]
    moon = ID_birth[4:6]
    day = ID_birth[6:8]
    print("生日: " + year + '年' + moon + '月' + day + '日')
    province=ID[0:2]
    city = ID[2:4]
    county = ID[4:6]
    print("你的出生地为:{}省{}市{}县".format(province,city,county))
    
    
    if int(ID_sex) % 2 == 0:
        print('性别:女')
    else:
        print('性别:男')
    
    # 此部分应为错误判断,如果错误就不应有上面的输出,如何实现?#
    W = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2]
    ID_num = [18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2]
    ID_CHECK = ['1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2']
    ID_aXw = 0
    for i in range(len(W)):
        ID_aXw = ID_aXw + int(ID[i]) * W[i]
    
    ID_Check = ID_aXw % 11
    if ID_check == ID_CHECK[ID_Check]:
        print('正确的身份证号码')
    else:
        print('错误的身份证号码')
    

      

    import  os
    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 = 'https://www.huya.com/g/lol'
    web.open_new_tab(url)
    for i in range(2, 4):
        web.open_new_tab('https://www.huya.com/g/lol'+str(i)+'.html')
    for i in range(2, 10):
       url = 'https://www.huya.com/g/lol{}.html'.format(i)
       print (url)
    

      

     

    2.英文词频统计预处理

    article ='''
    It is said that the true nature of being is veiled. The labor of words, 
    the expression of art, the seemingly ceaseless buzz that is human thought all have in common the need to get at what really is so. 
    The hope to draw close to and possess the truth of being can be a feverish one. 
    In some cases it can even be fatal, 
    if pleasure is one's truth and its attainment more important than life itself. 
    In other lives, though, the search for what is truthful gives life.
    '''
    
    split = article.split()
    print(split)
    
    #使用空格替换标点符号
    article = article.replace(",","").replace(".","").replace(":","").replace(";","").replace("?","")
    
    
    #大写字母转换成小写字母
    exchange = article.lower();
    print(exchange)
    
    #生成单词列表
    list = exchange.split()
    print(list)
    
    #生成词频统计
    dic = {}
    for i in list:
        count = list.count(i)
        dic[i] = count
    print(dic)
    

      

    3

    • 同一目录、绝对路径、相对路径
    • 凯撒密码:从文件读入密函,进行加密或解密,保存到文件。
    • 词频统计:下载一首英文的歌词或文章或小说,保存为utf8文件。从文件读入文本进行处理。
    file=open("1.txt",'r')
    text=file.read();
    file.close;
    cip = '';
    for i in text:
      cip += chr(ord(i)+6)
    print("明文加密后为:",cip)
    file=open("2.txt",'w')
    file.write(cip);
    file.close()
    

      

    4.函数定义

    对函数定义的还比较模糊,不能完成代码,希望在今后的课程中能好好理解。

  • 相关阅读:
    ValidateInput(false)与this.ValidateRequest = false无效的解决方案
    WPF ListView CellTemplate Border设置ListView单元格的边框
    MFC4简单的窗口重绘(非部分重绘)
    针对每个用户的作业(针对用户的定期事件)
    用JQUERY做大表单(多表单域)AJAX提交
    MFC2消息处理
    asp.net mvc实战学习笔记(1)
    NHibernate基础拾遗
    FLEX/FLASH冒泡事件与非冒泡事件
    MFC1创建窗体
  • 原文地址:https://www.cnblogs.com/lenkay/p/10508338.html
Copyright © 2011-2022 走看看