天天看點

字元串操作、檔案操作,英文詞頻統計預處理

本作業要求來自于:https://edu.cnblogs.com/campus/gzcc/GZCC-16SE1/homework/2684

一、字元串操作

(一)基本要求

  • 解析身份證号:生日、性别、出生地等。
  • 凱撒密碼編碼與解碼
  • 網址觀察與批量生成

(二)編碼實作

1、解析身份證

id=[]
'''存放身份證前兩位對應的省名'''
place={'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':'香港特别行政區','91':'澳門特别行政區',}


id=input('請輸入您的身份證号碼:')
print('waiting......')

#出生年月日
bir=[]
for i in id[6:14]:
    bir.append(i)
year= ''.join(bir[0:4])
morth= ''.join(bir[4:6])
day= ''.join(bir[6:8])

#出生地
place=place[''.join(id[0:2])]

#性别
sexNum=int(id[16])
if sexNum%2 == 1:
    sex='帥哥'
else:
    sex='美女'

#輸出資訊
print('性别:' + sex +',出生:' + year + '年' + morth + '月' + day + ',' + place + '人')      

2、凱撒加密

Str = input('請輸入;')
for line in Str:
        print('{}'.format(chr(ord(line) +3)), end="")      

3、網址觀察

print('下面是校園新聞~')
for i in range(5,10):
    print('http://news.gzcc.cn/html/xiaoyuanxinwen/{}.html'.format(i))      

(三)截圖展示

字元串操作、檔案操作,英文詞頻統計預處理
字元串操作、檔案操作,英文詞頻統計預處理
字元串操作、檔案操作,英文詞頻統計預處理

二、英文詞頻統計預處理

  • 下載下傳一首英文的歌詞或文章或小說。
  • 将所有大寫轉換為小寫
  • 将所有其他做分隔符(,.?!)替換為空格
  • 分隔出一個一個的單詞
  • 并統計單詞出現的次數。
text = '''
Well I wonder could it be
  When I was dreaming about you baby
  You were dreaming of me
  Call me crazy
  Call me blind
  To still be suffering is stupid after all of this time

  Did I lose my love to someone better
  And does she love you like I do
  I do, you know I really really do

  Well hey
  So much I need to say
  Been lonely since the day
  The day you went away

  So sad but true
  For me there's only you
  Been crying since the day
  the day you went away

  I remember date and time
  September twenty second
  Sunday twenty five after nine
  In the doorway with your case
  No longer shouting at each other
  There were tears on our faces

  And we were letting go of something special
  Something we'll never have again
  I know, I guess I really really know
'''
text1 = text.lower()
text2 =text1.split()
num =text2.count('i')
print(text2)
print('“i”出現的次數:',num)      
字元串操作、檔案操作,英文詞頻統計預處理

三、檔案操作

1、凱撒密碼:從檔案讀入密函,進行加密或解密,儲存到檔案。

2、詞頻統計:

    • 下載下傳一首英文的歌詞或文章或小說,儲存為utf8檔案。
    • 從檔案讀入文本。

1、凱撒密碼

print('運作該程式後,将加密儲存在新檔案')
#打舊檔案
file=open("test.txt",'r')
f=file.read()
code=''

for i in f:
    code=code+chr(ord(i)+3)
#打開新檔案
file = open("new_test.txt", 'w')
file.write(code)
file.close()      

2、詞頻統計

file = open("font.txt", "r")
words = []

for line in file:
    for word in line.replace('\n', '').lower().split(" "):
        words.append(word)

map = {}
for word in words:
    map[word] = map[word] + 1 if word in map.keys() else 1

for key in map:
    print(key + ": " + str(map[key]))      

原文:

字元串操作、檔案操作,英文詞頻統計預處理

密文:

字元串操作、檔案操作,英文詞頻統計預處理

 2、詞頻統計

font檔案(歌詞)

字元串操作、檔案操作,英文詞頻統計預處理

運作結果(未截全)

字元串操作、檔案操作,英文詞頻統計預處理

 四、函數定義

  • 加密函數
  • 解密函數
  • 讀文本函數
def jiami():
    Str = input('請輸入;')
    for line in Str:
        print('{}'.format(chr(ord(line) + 3)), end="")

def wenben():
    print('運作該程式後,将加密儲存在新檔案')
    # 打舊檔案
    file = open("test.txt", 'r')
    f = file.read()
    code = ''

    for i in f:
        code = code + chr(ord(i) + 3)
    # 打開新檔案
    file = open("new_test.txt", 'w')
    file.write(code)
    file.close()
      
wenben()      

運作wenben()函數後:

字元串操作、檔案操作,英文詞頻統計預處理