天天看點

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

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

1.字元串操作:

  • 解析身份證号:生日、性别、出生地等。
file = open('region.txt','r')
text = file.read()
file.close()
text = text.replace('\u3000','')
list = text.split('\n')
print(list)
Id=input('請輸入身份證号碼:')
for areaVerify in list:
        if Id[0:6] == areaVerify[0:6]:
            city = areaVerify[6:]
Birth_year = Id[6:10]
Birth_month = Id[10:12]
Birth_day = Id[12:14]
sex = '女' if int(Id[16]) % 2 == 0 else '男'
print('{},出生于{}年{}月{}日,{}人'.format(sex,Birth_year,Birth_month,Birth_day,city))      
字元串操作、檔案操作,英文詞頻統計預處理
  • 凱撒密碼編碼與解碼
Plaintext=input('請輸入明文:')
pas=''
pla=''
print('密文為:',end='')
for i in range(0, len(Plaintext)):
    if Plaintext[i] in ['x', 'y', 'z']:
        pas += chr(ord(Plaintext[i]) - 23)
    else:
        pas += chr(ord(Plaintext[i]) + 3)
print(pas,end='')
print('\n原文為:',end='')
for i in range(0,len(pas)):
    if pas[i] in ['a','b','c']:
        pla += chr(ord(pas[i]) + 23)
    else:
        pla += chr(ord(pas[i])-3)
print(pla,end='')
      

  

字元串操作、檔案操作,英文詞頻統計預處理
  • 網址觀察與批量生成 
for i in range(2,6):
    url='http://news.gzcc.cn/html/xiaoyuanxinwen/{}.html'.format(i)
    print(url)
import webbrowser as web
for i in range(2,6):
    web.open_new_tab('http://news.gzcc.cn/html/xiaoyuanxinwen/'+ str(i) +'.html')
      
字元串操作、檔案操作,英文詞頻統計預處理

 2.英文詞頻統計預處理

text='''For you I gave my life, my everything,
you were my reason why
And you were true to me,
and in our darkest hour,
your love set me free
You were the only one for me,
what love was meant to be
Forever you will be my love, all I need
Our dreams of life together gone,
I don’t know what went wrong
I will miss you forever my love
I feel so lonely without you
I don’t know what to do
Cause you were everything to me, yeah it’s true
The light that shined into my life
The fire that stirs desire
Burning out like a flame in the night
You were the only one for me,
what love was meant to be
Forever you will be my love, all I need
Our dreams of life together gone,
I don’t know what went wrong
I will miss you forever my love
And now, I walk alone
An empty heart, the precious moments gone
But I know that you are there
one day when heaven calls, I’ll see you again
when heaven calls, I’ll see you again'''
text = text.lower()
pun =  ',.?!\n'
for i in pun:
     text = text.replace(i,' ')
words = text.split(' ')
print(words)
word_count = {}
for word in words:
    if word in word_count.keys():
        word_count[word] += 1
    else:
        word_count[word] = 1
print('單詞                個數')
for i in word_count:
    print('{:<20}{:<8}'.format(i,word_count[i]))
      
字元串操作、檔案操作,英文詞頻統計預處理