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

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

作业要求:

1.字符串操作:

  • 解析身份证号:生日、性别、出生地等。
  • 凯撒密码编码与解码
  • 网址观察与批量生成

      1.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]

where = ID_add = ID[0:6]

print("生日: " + year + '' + moon + '' + day + '')

if int(ID_sex) % 2 == 0:

    print('性别:女')

else:

    print('性别:男')
print("地点:"+where)

运行截图:

 1.2凯撒密码编码与解码

源码:

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

运行截图:

 1.3网址观察与批量生成

源码:

for i in range(2,10):
    url='http://news.gzcc.cn/html/xiaoyuanxinwen/{}.html'.format(i)
    print(url)

运行截图:

2.英文词频统计预处理

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

源码:

url='''
  I know, I guess I really really know
  Why do we never know what we've got till it's gone
  How could I carry on the day you went away
  Cause I've been missing you so much I have to say
  just one last dance
'''
# print(url.lower() + url.replace(",","") + url.replace("'",""))
str1=url.lower()
str2=str1.replace(",","")
str3=str2.replace("'","")
print(str3)
print(str3.split());
print(str3.count('we'))

运行截图:

3.文件操作

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

源码:

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

运行截图

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

源码:

f=open('yw.txt','r',encoding='utf8')
text=f.read()
f.close()
print(text)

f=open(r'..venvljxd.txt','r',encoding='utf8')
text=f.read()
f.close()
print(text)

f=open(r'C:UsersAdministratorPycharmProjectsdvenvljxd.txt','r',encoding='utf8')
text=f.read()
f.close()
print(text)

运行截图:

 4.函数定义

  • 加密函数
  • def get_text():
        plaincode = 'abcd'
        cipher=''
        for i in plaincode:
            cipher=cipher+chr(ord(i) + 3)
        return cipher
    bigstr = get_text()
    print(bigstr)
      
  • 解密函数
  • def get_text():
        plaincode = 'defg'
        cipher=''
        for i in plaincode:
            cipher=cipher+chr(ord(i) -3)
        return cipher
    bigstr = get_text()
    print(bigstr)
      
  • 读文本函数
  • def get_text():
        with open('yw.txt', 'r', encoding='utf8',errors='ignore') as f:
            text = f.read()
        return text
    bigstr = get_text()
    print(bigstr)

 

原文地址:https://www.cnblogs.com/zhangqianqian/p/10497483.html