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

 

1.字符串操作:

  • 解析身份证号:生日、性别、出生地等。
ID = input('请输入身份证号码(18位): ')
if len(ID) == 18:
    print("你的身份证号码是 " + ID)
else:
    print("输入错误")
 
ID_address=ID[0:6]
ID_birth = ID[6:14]
ID_sex = ID[14:17]
year = ID_birth[0:4]
moon = ID_birth[4:6]
day = ID_birth[6:8]

if int(ID_sex) % 2 == 0:
    ID_sex=''
else:
   ID_sex = ''
print('出生地编号:{}  \n出生日期:{}年{}月{}日   \n性别:{}'.format(ID_address,year,moon,day,ID_sex))

 

  • 凯撒密码编码与解码
a =input('请输入字母:')
str=''
zimu = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']
a = a.lower()
for i in range(len(a)):
    if a[i] in zimu:
        a.split()
        temp = ord(a[i])
        num = (temp - 97 + 3) % 26
        str = chr(num + 97)
        print(str,end="")
    else:
        print(" ",end="")

 

 

  • 网址观察与批量生成
for i in range(19,23):
    url = 'http://news.gzcc.cn/html/2019/xibusudi_0304/109{}.html'.format(i)
    print(url)

 

2.英文词频统计预处理

  • 下载一首英文的歌词或文章或小说,保存为utf8文件。
  • 从文件读出字符串。
  • 将所有大写转换为小写
  • 将所有其他做分隔符(,.?!)替换为空格
  • 分隔出一个一个的单词
  • 并统计单词出现的次数。
f = open('text1.txt','r+')
text = f.read()
f.close()
text=text.lower()
sep=",.'?!"
for s in sep:
    text=text.replace(s,' ')
print(text.split())
print(text.count('baby'),text.count('she'))

 

posted on 2019-03-05 15:21  kenda_yellow  阅读(128)  评论(0编辑  收藏  举报