You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
48 lines
1.6 KiB
48 lines
1.6 KiB
#异常判断
|
|
import re
|
|
|
|
#使用中文的字符编码来判断文件的类型
|
|
def judgeFile(name):
|
|
#中文的字符编码
|
|
zhPattern = re.compile(u'[\u4e00-\u9fa5]+')
|
|
for line in open(name, encoding='utf-8'):
|
|
match = zhPattern.search(line)
|
|
if match == None:
|
|
return False
|
|
else:
|
|
return True
|
|
|
|
#当输入文件序号合法的情况下,再检查文件是否重复查询还是词频结果文件
|
|
def judgeNumber(x, FileAdict):
|
|
name = x.split('.')[0]
|
|
find = name[0: 3]
|
|
if find == 'res':
|
|
print("文件不可查询,该文件是词频结果文件")
|
|
print("------------------------------------")
|
|
return False
|
|
else:
|
|
res = "res" + x
|
|
if res in FileAdict.values():
|
|
print("该文件已经查询过")
|
|
print("------------------------------------")
|
|
return False
|
|
return True
|
|
|
|
#检查输入的文件序号是否合法
|
|
def checkNum(x,FileAdict):
|
|
#文件数目的字符个数是否符合输入的字符长度 或者输入为空
|
|
if len(str(len(FileAdict))) < len(x) or len(x) == 0:
|
|
return -1
|
|
num = 0
|
|
#将字符串每个字符进行检查,同时对合法的字符串生成整数型
|
|
for i in range(0, len(x)):
|
|
if ord(x[i]) - ord('0') >= 0 and ord(x[i]) - ord('0') <= 9:
|
|
num = ord(x[i]) - ord('0') + num * 10
|
|
else:
|
|
return -1
|
|
if num <= len(FileAdict) and num >= 1 :
|
|
return num
|
|
else:
|
|
print("输入的序号不在文件序号范围内")
|
|
print("------------------------------------")
|
|
return -1 |