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

1.字符串操作:

  • 解析身份证号:生日、性别、出生地等。
ID = input('请输入你的十八位身份证号码: ')
if len(ID) == 18:
    print("你的身份证号码是 " + ID)
else:
    print("你输入的身份证号码错误")

ID_area = ID[0:6]
ID_birth = ID[6:14]
ID_sex = ID[14:17]

sheng=ID_area[0:2]
shi=ID_area[2:4]
qu=ID_area[4:6]
year = ID_birth[0:4]
moon = ID_birth[4:6]
day = ID_birth[6:8]

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={"01":"广州市","02":"韶关市","03":"深圳市","04":"珠海市","05":"汕头市","06":"佛山市","07":"江门市","08":"湛江市","09":"茂名市","12":"肇庆市","13":"惠州市","14":"梅州市","15":"汕尾市","16":"河源市","17":"阳江市","18":"清远市","19":"东莞市","20":"中山市","51":"潮州市","52":"揭阳市","53":"云浮市"}
countryside={"00":"阳江市","01":"市辖区","02":"江城区","21":"阳西县","23":"阳东县","81":"阳春市"}
for i in area:
    if i==sheng:

        print("省份:"+area[str(i)]+' ',end='')
for a in city:
    if a==shi:
        print("城市:"+city[str(a)]+' ',end='')
for b in countryside:
    if b==qu:
        print("地区:"+countryside[str(b)])

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

if int(ID_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. 加密,2. 解密")
    choice = input("请选择:")
    if choice == "1":
        encryption()
    elif choice == "2":
        decryption()
    else:
        print (u"您的输入有误!")

  • 网址观察与批量生成
//网址生成
for i in range(2,11):
    url='http://news.gzcc.cn/html/xiaoyuanxinwen/{}.html'.format(i)
    print(url)
//网址查看
import webbrowser as web
url = 'http://news.gzcc.cn/html/xiaoyuanxinwen/2.html'
web.open_new_tab(url)

2.英文词频统计预处理

  • 下载一首英文的歌词或文章或小说。
  • 将所有大写转换为小写
  • 将所有其他做分隔符(,.?!)替换为空格
  • 分隔出一个一个的单词
  • 并统计单词出现的次数。
song='''I'm a big big girl  In a big big world It's not a big big thing if you leave me But I do do feel  that I do do will miss you much Miss you much  I can see the first leaf falling It's all yellow and nice  It's so very cold outside Like the way I'm feeling inside  I'm a big big girl In a big big world It's not a big big thing if you leave me But I do do feel  that I do do will miss you much Miss you much  Outside it's now raining
And tears are falling from my eyes  Why did it have to happen  Why did it all have to end I'm a big big girl  In a big big world It's not a big big thing if you leave me But I do do feel  that I do do will miss you much Miss you much  I have your arms around me warm like fire
But when I open my eyes   You're gone I'm a big big girl  In a big big world It's not a big big thing if you leave me But I do do feel  that I do do will miss you much Miss you much  I'm a big big girl
In a big big world It's not a big big thing if you leave me But I do feel that will miss you much  Miss you much'''
text={}
sign= "',.?!()-"
for i in sign:
    song = song.replace(i, ' ')
song=song.lower()
song=(song.split())
for i in song:
        if i not in text:
            text[i] = 1
        else:
            text[i] += 1
print(text)
print(song.count('eyes'))

 

3.文件操作

  • 同一目录、绝对路径、相对路径
//同一目录
fo=open('song.txt','r',encoding='utf8')
content=fo.read()
fo.close()
print(content,end='')
//绝对路径
fo=open(r'C:\Users\ASUS\PycharmProjects\seconde\venv\py\song.txt','r',encoding='utf8')
content=fo.read()
fo.close()
print(content,end='')
//相对路径
fo=open(r'./song.txt','r',encoding='utf8')
content=fo.read()
fo.close()
print(content,end='')

  • 凯撒密码:从文件读入密函,进行加密或解密,保存到文件。
def encode():
   fo = open(r'G:\大三下学期\python\song.txt', 'r')
   s = fo.read()
   str=''
   for i in s:
       str = str +chr(ord(i)+3)
       print(chr(ord(i) + 3), end='')
   fo.close()
   fo = open(r'G:\大三下学期\python\song.txt', 'w')
   fo.write(str)
   fo.close()
def decode():
    fo = open(r'G:\大三下学期\python\song.txt', 'r')
    s = fo.read()
    str = ''
    for i in s:
        str = str + chr(ord(i) - 3)
        print(chr(ord(i) - 3), end='')
    fo.close()
    fo = open(r'G:\大三下学期\python\song.txt', 'w')
    fo.write(str)
    fo.close()
def main():
    while True:
        print(u"1. 加密,2. 解密")
        choice = input("请选择:")
        if choice == "1":
            encode()
            print("")
        elif choice == "2":
            decode()
        else:
            print(u"您的输入有误!")
if __name__ == '__main__':
 main()

 

  • 词频统计:下载一首英文的歌词或文章或小说,保存为utf8文件。从文件读入文本进行处理。
text = {}
symbol = "',.?!()"
with open(r"G:\大三下学期\python\a.txt", "r", encoding='utf8') as f:
    lyric = f.read()
for i in symbol:
    lyric = lyric.replace(i,' ')
    lyric = lyric.lower()
    lyric = lyric.split()
    for i in lyric:
        if i not in text:
            text[i] = 1
        else:
            text[i] += 1
    print(text)

 

 4.函数定义

  • 加密函数
def encode():
   fo = open(r'G:\大三下学期\python\song.txt', 'r')
   s = fo.read()
   str=''
   for i in s:
       str = str +chr(ord(i)+3)
       print(chr(ord(i) + 3), end='')
   fo.close()
   fo = open(r'G:\大三下学期\python\song.txt', 'w')
   fo.write(str)
   fo.close()
  • 解密函数
def decode():
    fo = open(r'G:\大三下学期\python\song.txt', 'r')
    s = fo.read()
    str = ''
    for i in s:
        str = str + chr(ord(i) - 3)
        print(chr(ord(i) - 3), end='')
    fo.close()
    fo = open(r'G:\大三下学期\python\song.txt', 'w')
    fo.write(str)
    fo.close()
  • 读文本函数
def read_file():
    text = {}
    symbol = "',.?!()"
    with open(r"G:\大三下学期\python\a.txt", "r", encoding='utf8') as f:
        lyric = f.read()
    for i in symbol:
         lyric = lyric.replace(i,' ')
         lyric = lyric.lower()
         lyric = lyric.split()
    for i in lyric:
        if i not in text:
            text[i] = 1
        else:
            text[i] += 1
    print(text)