diff options
| author | Hui Lan <lanhui@zjnu.edu.cn> | 2020-03-10 12:57:09 +0800 | 
|---|---|---|
| committer | Hui Lan <lanhui@zjnu.edu.cn> | 2020-03-10 12:57:09 +0800 | 
| commit | 253a32c774e23afe4cbc7b98759f118c403ead1f (patch) | |
| tree | 8337a67fb0f0aef19f9f74bdad57a6bd27cb68f0 | |
| parent | 58c3198448aec1245f5480fed8b3e6308a0bdf6b (diff) | |
analyze.py: check if a score file contains BOM (byte order marker) before proceeding
| -rw-r--r-- | analyze.py | 10 | 
1 files changed, 10 insertions, 0 deletions
| @@ -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()
 | 
