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

这个作业的要求来自于:https://edu.cnblogs.com/campus/gzcc/GZCC-16SE1/homework/2684

1.字符串操作:

  • 解析身份证号:生日、性别、出生地等。
id=input("请输入身份证号:")
province=id[0:2]
city=id[2:4]
year=id[6:10]
month=id[10:12]
day=id[12:14]
sex=id[16]
if int(sex)%2 == 0:
    sex='女'
else : sex='男'
if len(id)==18:
    print('你输入的身份证号码为'+id)
    if province != '44':
        print('该身份证所属的地区在广东省外')
    else:
        if city != '19':
            print('该身份证所属的地区在广东省东莞市外')
        else:
            print('该身份证所属的地区为广东省东莞市')
    print('出生日期是' + year + '年' + month + '月' + day + '日')
    print( '性别为' + sex)
else:
    print('你输入的身份证号码有误')

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

  • 凯撒密码编码与解码
plaincode=input('letter:')
for i in plaincode:
    print(chr(ord(i)+10),end='')

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

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

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

2.英文词频统计预处理

  • 下载一首英文的歌词或文章或小说。
  • 将所有大写转换为小写
  • 将所有其他做分隔符(,.?!)替换为空格
  • 分隔出一个一个的单词
  • 并统计单词出现的次数。
f=open('faded.txt','r',encoding='utf8')
text=f.read()
text=text.lower()
print(text)
s=',.!?:'
for c in s:
    text=text.replace(c,'')
print(text.split())
list = text.split(" ")
lists=[]
for y in list:
    if y not in lists:
        lists.append(y)
for x in lists:
    print(x,list.count(x))
f.close()

  运行结果:

C:UsersAdministratorPycharmProjectsdvenvScriptspython.exe C:/Users/Administrator/PycharmProjects/bd/lxy.py
you were the shadow to my life
did you feel us
another start
you fade away
afraid our aim is out of sight
wanna see us
alive
where are you now
where are you now
where are you now
was it all in my fantasy
where are you now
were you only imaginary
where are you now
atlantis
under the sea
under the sea
where are you now
another dream
the monster's running wild inside of me
i'm faded
i'm faded
so lost i'm faded
i'm faded
so lost i'm faded
these shallow waters never met
what i needed
i'm letting go
a deeper dive
eternal silence of the sea
i'm breathing
alive
where are you now
where are you now
under the bright
but faded lights
you set my heart on fire
where are you now
where are you now
another dream
another dream
another dream
another dream
where are you now
atlantis
under the sea
under the sea
where are you now
another dream
the monster's running wild inside of me
i'm faded
i'm faded
so lost i'm faded
i'm faded
so lost i'm faded
['you', 'were', 'the', 'shadow', 'to', 'my', 'life', 'did', 'you', 'feel', 'us', 'another', 'start', 'you', 'fade', 'away', 'afraid', 'our', 'aim', 'is', 'out', 'of', 'sight', 'wanna', 'see', 'us', 'alive', 'where', 'are', 'you', 'now', 'where', 'are', 'you', 'now', 'where', 'are', 'you', 'now', 'was', 'it', 'all', 'in', 'my', 'fantasy', 'where', 'are', 'you', 'now', 'were', 'you', 'only', 'imaginary', 'where', 'are', 'you', 'now', 'atlantis', 'under', 'the', 'sea', 'under', 'the', 'sea', 'where', 'are', 'you', 'now', 'another', 'dream', 'the', "monster's", 'running', 'wild', 'inside', 'of', 'me', "i'm", 'faded', "i'm", 'faded', 'so', 'lost', "i'm", 'faded', "i'm", 'faded', 'so', 'lost', "i'm", 'faded', 'these', 'shallow', 'waters', 'never', 'met', 'what', 'i', 'needed', "i'm", 'letting', 'go', 'a', 'deeper', 'dive', 'eternal', 'silence', 'of', 'the', 'sea', "i'm", 'breathing', 'alive', 'where', 'are', 'you', 'now', 'where', 'are', 'you', 'now', 'under', 'the', 'bright', 'but', 'faded', 'lights', 'you', 'set', 'my', 'heart', 'on', 'fire', 'where', 'are', 'you', 'now', 'where', 'are', 'you', 'now', 'another', 'dream', 'another', 'dream', 'another', 'dream', 'another', 'dream', 'where', 'are', 'you', 'now', 'atlantis', 'under', 'the', 'sea', 'under', 'the', 'sea', 'where', 'are', 'you', 'now', 'another', 'dream', 'the', "monster's", 'running', 'wild', 'inside', 'of', 'me', "i'm", 'faded', "i'm", 'faded', 'so', 'lost', "i'm", 'faded', "i'm", 'faded', 'so', 'lost', "i'm", 'faded']
you 15
were 1
the 7
shadow 1
to 1
my 3
life
did 1
feel 1
us
another 1
start
you 1
fade 1
away
afraid 1
our 1
aim 1
is 1
out 1
of 4
sight
wanna 1
see 1
us
alive
where 1
are 12
now
where 4
now
was 1
it 1
all 1
in 1
fantasy
where 1
now
were 1
only 1
imaginary
where 1
now
atlantis
under 2
sea
under 2
sea
where 2
now
another 3
dream
the 2
monster's 2
running 2
wild 2
inside 2
me
i'm 2
faded
i'm 4
faded
so 4
lost 4
i'm 4
faded
these 1
shallow 1
waters 1
never 1
met
what 1
i 1
needed
i'm 1
letting 1
go
a 1
deeper 1
dive
eternal 1
silence 1
sea
i'm 1
breathing
alive
where 1
now
under 1
bright
but 1
faded 2
lights
you 1
set 1
heart 1
on 1
fire
where 1
dream
another 3
dream
where 1
 
Process finished with exit code 0

3.文件操作

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

 同一目录:

f=open('faded.txt','r',encoding='utf8') 
text=f.read()
print(text)
f.close()

绝对路径:

f=open(r'D:faded.txt','r',encoding='utf8') 
text=f.read()
print(text)
f.close()

相对路径: 

f=open('D:\faded.txt','r',encoding='utf8') 
text=f.read()
print(text)
f.close()

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

file=open("caesar.txt")
caesar=file.read()
print("加密前的密码:",caesar)
cipher='';
jiemi='';
for i in caesar:
    cipher=cipher+chr(ord(i)+3);
print("加密后的密码:",cipher)
file=open("cipher.txt",'w')
file.write(cipher)
file.close()

运行结果:

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

 加密前文件:

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

加密后文件:

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

 4.函数定义

  • 加密函数
  • def get_text():
        plaincode = 'abcd'
        cipher=''
        for i in plaincode:
            cipher=cipher+chr(ord(i) + 3)
        return cipher
    bigstr = get_text()
    print(bigstr)
  • 解密函数
  • def get_text():
        plaincode = 'defg'
        cipher=''
        for i in plaincode:
            cipher=cipher+chr(ord(i) -3)
        return cipher
    bigstr = get_text()
    print(bigstr)
    

      

  • 读文本函数
  • def get_text():
        with open('yw.txt', 'r', encoding='utf8',errors='ignore') as f:
            text = f.read()
        return text
    bigstr = get_text()
    print(bigstr)