summaryrefslogtreecommitdiff
path: root/analyze.py
diff options
context:
space:
mode:
authorHui Lan <lanhui@zjnu.edu.cn>2020-03-10 12:57:09 +0800
committerHui Lan <lanhui@zjnu.edu.cn>2020-03-10 12:57:09 +0800
commit253a32c774e23afe4cbc7b98759f118c403ead1f (patch)
tree8337a67fb0f0aef19f9f74bdad57a6bd27cb68f0 /analyze.py
parent58c3198448aec1245f5480fed8b3e6308a0bdf6b (diff)
analyze.py: check if a score file contains BOM (byte order marker) before proceeding
Diffstat (limited to 'analyze.py')
-rw-r--r--analyze.py10
1 files changed, 10 insertions, 0 deletions
diff --git a/analyze.py b/analyze.py
index ce1e7f3..778d5d2 100644
--- a/analyze.py
+++ b/analyze.py
@@ -43,6 +43,7 @@ import json, os, sys
# Solve UnicodeDecodeError - https://blog.csdn.net/blmoistawinde/article/details/87717065
import _locale
+import codecs
_locale._getdefaultlocale = (lambda *args: ['zh_CN', 'utf8'])
@@ -54,6 +55,7 @@ def get_task_information(fname):
def get_student_information(fname):
result = []
+
with open(fname) as f:
for line in f:
line = line.strip()
@@ -78,6 +80,14 @@ def get_max_score(d):
def get_student_number(fname):
d = {}
+
+ # If a file has BOM (Byte Order Marker) charater, stop.
+ with open(fname, 'r+b') as f:
+ s = f.read()
+ if s.startswith(codecs.BOM_UTF8):
+ print('\nERROR: The file %s contains BOM character. Remove that first.' % (fname))
+ sys.exit()
+
f = open(fname)
for line in f:
line = line.strip()