博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
字符串操作、文件操作,英文词频统计预处理
阅读量:4669 次
发布时间:2019-06-09

本文共 5287 字,大约阅读时间需要 17 分钟。

作业要求来自:

1.字符串操作:

解析身份证号:生日、性别、出生地等。

1 def analysis_idcard(id_number): 2     print(id_number[0:2]+'代表省份的代码') 3     print(id_number[2:4] + '代表城市的代码') 4     print(id_number[4:6] + '代表区县的代码') 5     print(id_number[6:14] + '代表出生年月日的代码') 6     print(id_number[14:16] + '代表所在派出所的的代码') 7     print(id_number[-2] + '代表性别(奇数男,偶数女)') 8     print(id_number[-1] + '代表校验码') 9 id_code = input()10 analysis_idcard(id_code)

 

凯撒密码编码与解码

1 def kaisa_encode(str): 2     for i in str: 3         print(chr(ord(i)+3),end='') 4 oldstr = input() 5 kaisa_encode(oldstr) 6  7 def kaisa_decode(str): 8     for i in str: 9         print(chr(ord(i)-3),end='')10 oldstr = input()11 kaisa_decode(oldstr)

 

网址观察与批量生成

1 url = r'http://news.gzcc.cn/html/xiaoyuanxinwen/'2 print(url)3 for i in range(254):4     if i > 1:5         print(url + str(i) + '.html')

 

2.英文词频统计预处理

下载一首英文的歌词或文章或小说。

将所有大写转换为小写

将所有其他做分隔符(,.?!)替换为空格

分隔出一个一个的单词

并统计单词出现的次数。

1 lyric = '''It starts with one thing  2 I don't know why  3 It doesn't even matter  4 How hard you try  5 Keep that in mind  6 I designed this rhyme  7 To explain in due time  8 All I know  9 Time is a valuable thing 10 Watch it fly by 11 As the pendulum swings 12 Watch it count down 13 To the end of the day 14 The clock ticks life away 15 It's so unreal 16 Didn't look out below 17 Watch the time go 18 Right out the window 19 Trying to hold on 20 But didn't even know 21 Wasted it all just 22 To watch you go 23 I kept everything inside and 24 Even though I tried 25 It all fell apart 26 What it meant to me will 27 Eventually be a 28 Memory of a time when 29 I tried so hard 30 And got so far 31 But in the end 32 It doesn't even matter 33 I had to fall 34 To lose it all 35 But in the end 36 It doesn't even matter 37 One thing 38 I don't know why 39 It doesn t even matter 40 How hard you try 41 Keep that in mind 42 I designed this rhyme 43 To remind myself how 44 I tried so hard 45 In spite of the way 46 You were mocking me 47 Acting like I was 48 Part of your property 49 Remembering all the 50 Times you fought with me 51 I'm surprised it got so (far) 52 Things aren't the way 53 They were before 54 You wouldn't even 55 Recognise me anymore 56 Not that you 57 Knew me back then 58 But it all comes 59 Back to me (in the end) 60 You kept everything inside 61 And even though I tried 62 It all fell apart 63 What it meant to me will 64 Eventually be a 65 Memory of a time when I 66 I tried so hard 67 And got so far 68 But in the end 69 It doesn't even matter 70 I had to fall 71 To lose it all 72 But in the end 73 It doesn't even matter 74 I've put my trust in you 75 Pushed as far as I can go 76 For all this 77 There's only one thing you should know 78 I've put my trust in you 79 Pushed as far as I can go 80 For all this 81 There's only one thing you should know 82 I tried so hard 83 And got so far 84 But in the end 85 It doesn't even matter 86 I had to fall 87 To lose it all 88 But in the end 89 It doesn't even matter''' 90 symbol = "',.?!()" 91 for i in symbol: 92     lyric = lyric.replace(i, ' ') 93 word = {} 94 lyric = lyric.lower() 95 lyric = lyric.split() 96 for i in lyric: 97     if i not in word: 98         word[i] = 1 99     else:100         word[i] +=1101 print(word)
View Code

运行结果

3.文件操作

同一目录、绝对路径、相对路径

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

1 def my_encode(): 2     code='' 3     with open(r'E:\Pythonproject\code.txt', 'r', encoding='utf8') as f: 4         for i in f.read(): 5             code = code + (chr(ord(i) + 3)) 6     with open("code.txt", "w", encoding='utf8') as f: 7         f.write(code) 8  9 def my_decode():10     code = ''11     with open(r'./code.txt', 'r', encoding='utf8') as f:12         for i in f.read():13             code = code + (chr(ord(i) - 3))14     with open("code.txt", "w", encoding='utf8') as f:15         f.write(code.replace(r'', '\n'))

运行效果

原始

加密

解密

 

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

1 def count_word(): 2     word = {} 3     symbol = "',.?!()" 4     with open("code.txt", "r", encoding='utf8') as f: 5         lyric = f.read() 6     for i in symbol: 7         lyric = lyric.replace(i, ' ') 8     lyric = lyric.lower() 9     lyric = lyric.split()10     for i in lyric:11         if i not in word:12             word[i] = 113         else:14             word[i] += 115     print(word)

运行效果

 4.函数定义

加密函数

解密函数

读文本函数

1 def my_encode(): 2     code='' 3     with open(r'E:\Pythonproject\code.txt', 'r', encoding='utf8') as f: 4         for i in f.read(): 5             code = code + (chr(ord(i) + 3)) 6     with open("code.txt", "w", encoding='utf8') as f: 7         f.write(code) 8  9 def my_decode():10     code = ''11     with open(r'./code.txt', 'r', encoding='utf8') as f:12         for i in f.read():13             code = code + (chr(ord(i) - 3))14     with open("code.txt", "w", encoding='utf8') as f:15         f.write(code.replace(r'', '\n'))16 17 def count_word():18     word = {}19     symbol = "',.?!()"20     with open("code.txt", "r", encoding='utf8') as f:21         lyric = f.read()22     for i in symbol:23         lyric = lyric.replace(i, ' ')24     lyric = lyric.lower()25     lyric = lyric.split()26     for i in lyric:27         if i not in word:28             word[i] = 129         else:30             word[i] += 131     print(word)
View Code

 

转载于:https://www.cnblogs.com/-QAQ/p/10497302.html

你可能感兴趣的文章
Node.js区块链开发pdf
查看>>
轻松学SQL Server数据库pdf
查看>>
Oracle 日期查询
查看>>
说说今年的计划
查看>>
把discuzX 的用户登录信息添加到纯静态页面
查看>>
文件大小计算
查看>>
iOS:给图片置灰色
查看>>
Java 8 (5) Stream 流 - 收集数据
查看>>
ubuntu下安装JDK
查看>>
【C#】使用DWM实现无边框窗体阴影或全透窗体
查看>>
【MySql】脚本备份数据库
查看>>
keil5 配置 stm32f103rc 软件仿真
查看>>
RESTful到底是什么玩意??
查看>>
Oracle创建视图的一个问题
查看>>
(一)线性表
查看>>
hdu 1003 Max Sum (DP)
查看>>
mysql增备
查看>>
[APIO2015]雅加达的摩天楼
查看>>
andorid之帧布局FrameLayout
查看>>
(转,记录用)jQuery页面加载初始化的3种方法
查看>>