字符串操作、文件操作

1.字符串操作:

  • 解析身份证号:生日、性别、出生地等。
  • number='441801199710232652'
    print('身份证号码:'+number)
    sr=""
    for i in number[0:2]:
        sr=sr+i
    print(sr+":所在省市")
    sr=""
    for i in number[2:4]:
        sr=sr+i
    print(sr+":所在地区")
    sr=""
    for i in number[4:6]:
        sr=sr+i
    print(sr+":所在县区")
    sr=""
    for i in number[6:14]:
        sr=sr+i
    print(sr+":出生日期:"+sr[0:4]+""+sr[4:6]+""+sr[6:8]+"")
    sr=""
    for i in number[14:16]:
        sr=sr+i
    print(sr+":户口所归属的派出所")
    print(number[16]+":性别(男为奇数,女为偶数)")
    print(number[17]+":校验位")

  • 凯撒密码编码与解码
  • s=''
    demo='Tom小学生'
    print('明文:'+demo)
    for j in  demo:
        s=s+chr(ord(j)+3)
    print('加密后:'+s)
    ss=''
    for k in s:
        ss=ss+chr(ord(k)-3)
    print('解密后:'+ss)

  • 网址观察与批量生成
  • href='http://news.gzcc.cn/html/xiaoyuanxinwen/index.html'
    for o in range(1,11):#254
        if(o==1):
            print(href)
        else:
            print('http://news.gzcc.cn/html/xiaoyuanxinwen/'+str(o)+'.html')

 

2.英文词频统计预处理

  • 下载一首英文的歌词或文章或小说。
  • 将所有大写转换为小写
  • 将所有其他做分隔符(,.?!)替换为空格
  • 分隔出一个一个的单词
  • 并统计单词出现的次数。
  • song = song.lower()
    x=',...\n?!'
    for xx in x:
        song=song.replace(xx,' ')
    print(song.split(' '))
    for n in song.split(' '):
        print(n+':'+str(song.count(n)))

 

3.文件操作

  • 同一目录、绝对路径、相对路径
  • 凯撒密码:从文件读入密函,进行加密或解密,保存到文件。
  • text1=open('Tom.txt','r',encoding='utf-8')
    text1=text1.read()
    text11=''
    for t in  text1:
        text11=text11+chr(ord(t)+3)
    text2=open('Tom1.txt','a',encoding='utf-8')
    text2=text2.write(text11)

  • 词频统计:下载一首英文的歌词或文章或小说,保存为utf8文件。从文件读入文本进行处理。
  • text=open('gril.txt','r',encoding='utf-8')
    text=text.read()
    x=",?!."
    for xx in x:
        text1=text.replace(xx,' ')
    print(text1)
    text2=open('gril1.txt','a',encoding='utf-8')
    text2.write(text1)

 

 4.函数定义

  • 加密函数
  • def encryption(text):
        s=''
        for i in text:
            s=s+chr(ord(i)+3)
        return s

     

  • 解密函数
  • def decrypt(text):
        s = ''
        for i in text:
            s = s + chr(ord(i)-3)
        return s

     

  • 读文本函数
  • def readFile(filePath):
        file=open(filePath,'r',encoding='utf-8')
        return file.read()

     

posted @ 2019-03-10 14:00  曾鹏菲  阅读(264)  评论(0编辑  收藏  举报