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

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

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]
    province = ID_add[0:2]
    area = {"11": "北京", "12": "天津", "13": "河北", "14": "山西", "15": "内蒙古", "21": "辽宁", "22": "吉林", "23": "黑龙江", "31": "上海",
            "32": "江苏", "33": "浙江", "34": "安徽", "35": "福建", "36": "江西", "37": "山东", "41": "河南", "42": "湖北", "43": "湖南",
            "44": "广东", "45": "广西", "46": "海南", "50": "重庆", "51": "四川", "52": "贵州", "53": "云南", "54": "**", "61": "陕西",
            "62": "甘肃", "63": "青海", "64": "宁夏", "65": "新疆", "71": "台湾", "81": "香港", "82": "澳门", "91": "国外"}
    
    city = ID_add[2:4]
    area2 = {"01": "广州", "02": "韶关", "03": "深圳", "04": "珠海", "05": "汕头", "06": "佛山", "07": "江门", "08": "湛江", "09": "茂名"}
    
    county = ID_add[4:6]
    area3 = {"02": "茂南", "03": "茂港", "23": "电白", "81": "高州", "82": "化州", "83": "信宜"}
    
    year = ID_birth[0:4]
    moon = ID_birth[4:6]
    day = ID_birth[6:8]
    print("你的身份证出生地为:"+ area[province] + area2[city] + area3[county])
    print("生日: " + year + '' + moon + '' + day + '')
    if int(ID_sex) % 2 == 0:
        print('性别:女')
    else:
        print('性别:男')

  • 凯撒密码编码与解码
  • MAX_KEY_SIZE = 26
    
    def getMode():
        while True:
            print('请选择加密或解密模式:')
            print('加密:encrypt(e)')
            print('解密:decrypt(d)')
            mode = input().lower()
            if mode in 'encrypt e decrypt d '.split():
                return mode
            else:
                print('请输入"encrypt"或"e"或"decrypt"或"d"!')
    def getMessage():
        print('请输入你的信息:')
        return input()
    
    def getKey():
        key = 0
        while True:
            print('请输入密钥数字(1-%s)' % (MAX_KEY_SIZE))
            key = int(input())
            if (key >=1 and key <= MAX_KEY_SIZE):
                return key
    
    def getTranslatedMessage(mode, message, key):
        if mode[0] == 'd':
            key = -key
        translated = ''
        for symbol in message:
            if symbol.isalpha():
                num = ord(symbol)
                num += key
                if symbol.isupper():
                    if num > ord('Z'):
                        num -= 26
                    elif num < ord('A'):
                        num += 26
                elif symbol.islower():
                    if num > ord('z'):
                        num -= 26
                    elif num < ord('a'):
                        num += 26
    
                translated += chr(num)
            else:
                translated += symbol
        return translated
    
    mode = getMode()
    message = getMessage()
    if mode[0] != 'b':
        key = getKey()
    
    print('你要翻译的信息是:')
    if mode[0] != 'b':
        print(getTranslatedMessage(mode, message, key))
    else:
        for key in range(1, MAX_KEY_SIZE + 1):
            print(key, getTranslatedMessage('decrypt', message, key))

     

  • 网址观察与批量生成
  • for i in range(2,10):
        url='http://www.sogou.com/{}.html'.format(i)
        print(url)

     

 

2.英文词频统计预处理:

  • 下载一首英文的歌词或文章或小说。
  • 将所有大写转换为小写
  • 将所有其他做分隔符(,.?!)替换为空格
  • 分隔出一个一个的单词
  • 并统计单词出现的次数。
  • area='''It seems that we ain't spoke in so long
    You looking really good
    Hear you doing mighty well for yourself
    Know what I did was so wrong
    And i'm paying every day
    For the way I mistreated you girl
    How can it be over
    Cause girl I ain't ready to just give this up
    Gotta find a way to get back your heart
    Gotta take it back to the start
    Without you i'm falling apart
    Can't let it go girl, go girl
    (after everything been through)
    Can't let it go girl go girl
    (i know I still love you)
    Can't let it go girl, go girl
    Can't let you go
    Can't let you go girl, go girl
    (cause after all the years we been together)
    Can't let you go girl, go girl
    (girl I know we're meant to be forever)
    Can't let it go girl, go girl
    Can't let you go
    Girl I know I put you through alot
    Played with your heart on occasion
    Left it scarred from the shit that I did
    Oh but I know now your worth all I got
    Gotta give this a shot
    Cause we can't just be done
    How can it be over
    Cause girl I ain't ready to just give this up
    Gotta find a way to get back your heart
    Gotta take it back to the start
    Without you girl i'm falling, falling apart'''
    area = area.lower()
    yt = "',.?!()-"
    for i in yt:
        area = area.replace(i, ' ')
    out = {}
    area = (area.split())
    for i in area:
            if i not in out:
                out[i] = 1
            else:
                out[i] += 1
    print(out)

     

 

3.文件操作:

  • 同一目录、绝对路径、相对路径
  • fo=open('book.txt','r',encoding='utf8')
    content=fo.read()
    fo.close()
    print(content,end='')

     

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

 

转载于:https://www.cnblogs.com/wytai/p/10513446.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值