-
Notifications
You must be signed in to change notification settings - Fork 40
/
judge.py
executable file
·53 lines (48 loc) · 1.31 KB
/
judge.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#!/usr/bin/env python
#coding=utf-8
from optparse import OptionParser
#写入文件函数
def writeFile(fp, arr):
for ele in arr:
if ele != arr[-1:]:
fp.write(ele+'\n')
else:
fp.write(ele)
#判断是否选择csv文件格式
def judgeCsv(options, text):
wordArr = []
for line in text.readlines():
if options.csv != None:
ele = line.split(',')
for e in ele:
wordArr.append(e)
else:
wordArr.append(line.strip('\n'))
return wordArr
#判断是否为数字
def judgeNum(options, wordArr, func):
resArr = []
if options.num != None:
try:
resArr = func(wordArr, int(options.num))
return resArr
except:
exit("error:请输入一个数字")
else:
if func.__name__ == "social":
resArr = func(wordArr)
elif func == "frequency":
resArr = func(wordArr, 100)
else:
resArr = func(wordArr, 4)
return resArr
#判断是否输入文件名
def judgeOut(options, resArr):
if options.output != None:
fp = open(options.output, 'w+')
writeFile(fp, resArr)
fp.close()
else:
fp = open("output.txt", "w+")
writeFile(fp, resArr)
fp.close()