码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2019-03-11 00:59:46      阅读:173      评论:0      收藏:0      [点我收藏+]

标签:not   .com   hat   次数   coding   src   绝对路径   处理   temp   

1.字符串操作:

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

解析身份证信息:

 

ID=input("输入你的身份证号:")
shengfen = ID[0:2]
year = ID[6:10]
month = ID[10:12]
day = ID[12:14]
sex=ID[-2]

print(‘你的省份信息为:‘+shengfen)
print(‘出生日期为:‘+year+‘年‘+day+‘月‘+day+‘日‘)
if (int(sex) % 2) == 0:
    print(‘性别为:女性‘)
else:
    print(‘性别为:男性‘)

  技术图片

 

凯撒密码:

from idna import unichr

a=‘‘
s=input(‘输入要加密的信息:‘)
num=input(‘输入加密数字:‘)
print(‘密文为:‘)
for i in s:
    a+=unichr(ord(i)+int(num));
print(a)
print(‘还原信息:‘)
for i in a:
    print(unichr(ord(i)-int(num)),end=‘‘);

  技术图片

 

2.英文词频统计预处理

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

 

import operator
text=‘‘‘It was a cold winter day in 1919. A small boy was walking along the street in London.
His name was Tom. He was very hungry.
 He wanted to buy some bread, but he had no money.
 What could he do? When he was very young, he wanted to be a great man in the world of films.
 So he worked to sing and dance well.
 Thirty years later, the boy became one of the famous people in the world.‘‘‘

text1=text.replace(‘.‘,‘ ‘).replace(‘?‘,‘ ‘).replace(‘,‘,‘ ‘).lower().split();
dic = {}
for word in text1:
    if word not in dic:
        dic[word] = 1;
    else:
        dic[word] = dic[word] + 1;

swd = sorted(dic.items(), key=operator.itemgetter(1), reverse=True)
print(swd)

  技术图片

 

3.文件操作

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

凯撒密码:从文件读入密函,进行加密或解密,保存到文件。

from idna import unichr

def openFile(i):
    if i ==1:
        return open(r‘C:\Users\Shinelon\Desktop\123.txt‘, ‘r‘, encoding=‘gb2312‘);
    else:
        return open(r‘C:\Users\Shinelon\Desktop\234.txt‘, ‘a‘, encoding=‘utf8‘);

a=‘‘
f=openFile(1)
text=f.read();
f.close();
num=3
print(‘密文为:‘)
for i in text:
    a+=unichr(ord(i)+int(num));
print(a)
j=openFile(2)
j.seek(0)
j.truncate()
j.write(a)
j.close()
print(‘还原信息:‘)
for i in a:
    print(unichr(ord(i)-int(num)),end=‘‘);

  技术图片

 

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

import operator

f=open(r‘C:\Users\Shinelon\Desktop\123.txt‘,‘r‘,encoding=‘utf8‘);
text=f.read();
f.close();
text1=text.replace(‘.‘,‘ ‘).replace(‘?‘,‘ ‘).replace(‘,‘,‘ ‘).lower().split();
dic = {}
for word in text1:
    if word not in dic:
        dic[word] = 1;
    else:
        dic[word] = dic[word] + 1;

swd = sorted(dic.items(), key=operator.itemgetter(1), reverse=True)
print(swd)

  技术图片

 

 4.函数定义

  • 加密函数
  • 解密函数
  • 读文本函数

加密函数

def jiami(xinxi,num):
    temp=‘‘
    for i in xinxi:
        temp+=unichr(ord(i)+int(num));
    return temp;

  

解密函数

def jiemi(xinxi,num):
    temp=‘‘
    for i in xinxi:
        temp+=unichr(ord(i)-int(num));
    return temp;

  

读文本函数

def openFile():

        return open(r‘C:\Users\Shinelon\Desktop\123.txt‘, ‘r‘, encoding=‘utf8‘);

  

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

标签:not   .com   hat   次数   coding   src   绝对路径   处理   temp   

原文地址:https://www.cnblogs.com/chenshijiong/p/10508205.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!