Last active
January 29, 2016 15:43
-
-
Save sekika/20f65cb7e2eed1cd84d4 to your computer and use it in GitHub Desktop.
東洋大学教員用成績登録ファイル作成スクリプト
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# coding: utf-8 | |
# (東洋大学教員専用スクリプト) | |
# csv ファイルから、東洋大学の成績登録用テキストファイルを作成 | |
# http://sekika.github.io/2016/01/23/Seiseki/ | |
# | |
# python3 をインストールして実行 | |
# | |
# 使い方:seiseki.py 入力ファイル名 出力ファイル名 | |
# | |
# 「入力ファイル名」は成績のcsvファイル、1行目は学籍番号、2行目は成績 | |
# 「出力ファイル名」は成績一括登録用のテキストファイル | |
import sys | |
import re | |
id = re.compile('^[0-9]{10}$') # 学籍番号 (数字10桁) の正規表現 | |
infile = sys.argv[1] # 入力ファイル名 | |
outfile = sys.argv[2] # 出力ファイル名 | |
data = open(infile, 'r') # 入力ファイルを開く | |
output = open(outfile, 'w', encoding='utf-16') # 出力ファイルを UTF-16 で開く | |
row = 0 # 行番号 | |
for line in data: # 入力ファイルから1行ずつ読み込んで処理 | |
s=line.strip().split(',')[0:2] # 「,」区切りで最初の2列のデータを読む | |
row+=1 # 行番号に1を足す | |
s[0]=s[0].strip() # 最後にスペースが入っていたら削除 | |
s[1]=s[1].replace('/','*').strip() # 同上、「/」は「*」に変換 | |
if id.match(s[0]): # 学籍番号とマッチするかチェック | |
output.write("\t".join(s)) # タブ区切りで1行に連結して出力 | |
output.write('\r\n') # 改行は CR/LF | |
else: | |
print('Error in line', row, ': ID', s[0]) # 学籍番号形式エラー出力 | |
data.close() | |
output.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment