# coding=utf8
__author__ = 'Administrator'
import os
"""
#编写函数,计算字符串匹配的准确率
def Rate(origin,userInput):
if not (isinstance(origin,str) and isinstance(userInput,str)):
print('The two parameters must be strings.')
return
right = sum((1 for o, u in zip(origin,userInput) if o == u))
return round(right/len(origin),2)
s1 = 'Readability counts.'
s2 = 'readability count.'
print(Rate(s1,s2))
"""
s="fdgdfd,..,/[]"
import re
def remove_special(s):
new=""
for i in s:
if re.search(u'^[\\\_.a-zA-Z0-9\u4e00-\u9fa5]+$', i):
new+=i
return new
remove_special(s)
def compare_str(s1,s2):
if s1==s2:
return False
s1=s1.replace("1","")
s1=s1.replace("xxx","")
s2=s2.replace("1","")
s2=s2.replace("xxx","")
count=0
for a in s1:
if a in s2:
count+=1
if count>30:
return True
else:
return False
# 过滤非法字符
def sql_filter(sql, max_length=20):
dirty_stuff = ["\"", "\\", "/", "|", "&", "*", "'", "=", "-", "#", ";", "<", ">", "+", "%", "$", "(", ")", "%", "@","!"]
for stuff in dirty_stuff:
sql = sql.replace(stuff, "")
return sql[:max_length]
def filter(sql):
dirty_stuff = ["\"", "\\", "/", "|", "&", "*", "'", "=", "-", "#", ";", "<", ">", "+", "%", "$", "(", ")", "%", "@","!"]
for stuff in dirty_stuff:
sql = sql.replace(stuff, "")
return sql
path1="D:\Other\OtherMovie"
file_name_list=[]
file_same_name_dict={}
count=0
for dirpath, dirnames, filenames in os.walk(path1):
for filename in filenames:
full_path=os.path.join(dirpath, filename)
stat_info = os.stat(full_path)
file_same_name_dict[filename]=full_path
file_name_list.append(filename)
for base_name in file_name_list:
for a in file_name_list:
r= compare_str(base_name,a)
if r:
x=remove_special(base_name)
y=remove_special(a)
print(x+" "+y)
"""
username = "1234567890!@#!@#!@#$%======$%|&***"
username = sql_filter(username) # SQL注入
print(username)# 输出结果是:1234567890
import re
# username = "1234567890!@#!@#!@#$%======$%|&***"
username = "1234567890*"
username = "琅琊榜04.mp4"
username = "xxx"
# 检测到非法字符进入if
if not re.search(u'^[\\_.a-zA-Z0-9\u4e00-\u9fa5]+$', username):
msg = u"用户名不可以包含非法字符(!,@,#,$,%...)"
new=""
print(msg)
for i in username:
if re.search(u'^[\\\_.a-zA-Z0-9\u4e00-\u9fa5]+$', i):
new+=i
print(new)
"""