Skip to content

Instantly share code, notes, and snippets.

@lili668668
Last active Dec 23, 2017
Embed
What would you like to do?
合併 學生名單 與 組別名單 與 用學號加分系統 與 zuvio 成績登計系統
const fs = require('fs')
const csv = require('fast-csv')
var addscore = fs.readFileSync("./addscore.csv").toString()
var students = fs.readFileSync("./student.csv").toString()
var arr = {}
var wa = []
csv
.fromString(students, {headers: true})
.on("data", (data) => {
arr[data["學號"]] = {"姓名": data["姓名"], "分數": 0}
})
.on("end", () => {
csv
.fromString(addscore, {headers: true})
.on("data", (addscore_data) => {
if (arr[addscore_data["學號"]]) {
arr[addscore_data["學號"]]["分數"] = 1
} else {
console.log(addscore_data["學號"])
}
})
.on("end", () => {
Object.keys(arr).forEach((e, i, a) => {
var it = {"學號": e, "姓名": arr[e]["姓名"], "分數": arr[e]["分數"]}
wa.push(it)
})
var ws = fs.createWriteStream("new_add_score.csv")
csv.write(wa, {headers: true}).pipe(ws)
})
})
import csv
import argparse
header1 = "組別"
header2 = "組名"
parser = argparse.ArgumentParser(description="Input the csv file path.")
parser.add_argument('file_path', metavar="file path", type=str, nargs=2)
args = parser.parse_args()
group_list_file = args.file_path[0]
student_list_file = args.file_path[1]
with open(group_list_file, newline="", encoding="utf-8") as group_list_csv:
with open(student_list_file, newline="", encoding="utf-8") as student_list_csv:
new_file_path = "new.csv";
with open(new_file_path, "w", newline="", encoding="utf-8") as new_csv:
map_sid = {}
student_list_reader = csv.DictReader(student_list_csv)
for row in student_list_reader:
map_sid[row["學號"]] = {"學號": row["學號"], "姓名": row["姓名"]}
group_list_reader = csv.DictReader(group_list_csv)
for row in group_list_reader:
code = row["組別"]
gname = row["組名"]
sids = [row["組長的學號"], row["組員二號的學號"], row["組員三號的學號"], row["組員四號的學號"], row["組員五號的學號"]]
for sid in sids:
if map_sid.get(sid) != None:
map_sid[sid][header1] = code
map_sid[sid][header2] = gname
fieldnames = student_list_reader.fieldnames
fieldnames.extend(['組別', '組名'])
writer = csv.DictWriter(new_csv, fieldnames=fieldnames)
writer.writeheader()
for key, value in map_sid.items():
writer.writerow(value)
// zuvio 點名用的合併表單
const fs = require('fs')
const csv = require('fast-csv')
var attend = fs.readFileSync("./attend.txt").toString()
var students = fs.readFileSync("./student.csv").toString()
var att = JSON.parse(attend)
var arr = {}
var wa = []
csv
.fromString(students, {headers: true})
.on("data", (data) => {
arr[data["姓名"]] = {"學號": data["學號"], "分數": 0}
})
.on("end", () => {
var at = att.rollcall.enrollments_array;
Object.keys(at).forEach((e, i, a) => {
var index = e.trim()
var name = (at[index].last_name + at[e].first_name).trim()
if (arr[name]) {
if (at[index].type == "punctual") {
arr[name]["分數"] = 1
}
}
})
Object.keys(arr).forEach((e, i, a) => {
var it = {"學號": arr[e]["學號"], "姓名": e, "分數": arr[e]["分數"]}
wa.push(it)
})
var ws = fs.createWriteStream("new.csv")
csv.write(wa, {headers: true}).pipe(ws)
})
const fs = require('fs')
const csv = require('fast-csv')
var score = fs.readFileSync("./zuvio.csv").toString()
var students = fs.readFileSync("./student.csv").toString()
var arr = {}
var wa = []
csv
.fromString(students, {headers: true})
.on("data", (data) => {
arr[data["姓名"]] = {"學號": data["學號"], "分數": 0}
})
.on("end", () => {
csv
.fromString(score, {headers: true})
.on("data", (score_data) => {
if (arr[score_data["姓名"]]) {
arr[score_data["姓名"]]["分數"] = score_data["分數"]
} else {
console.log(score_data["姓名"])
}
})
.on("end", () => {
Object.keys(arr).forEach((e, i, a) => {
var it = {"學號": arr[e]["學號"], "姓名": e, "分數": arr[e]["分數"]}
wa.push(it)
})
var ws = fs.createWriteStream("zuvio_score.csv")
csv.write(wa, {headers: true}).pipe(ws)
})
})
const fs = require('fs')
const csv = require('fast-csv')
var score = fs.readFileSync("./1know.json").toString()
var students = fs.readFileSync("./student_1know.csv").toString()
var arr = {}
var wa = []
csv
.fromString(students, {headers: true})
.on("data", (data) => {
arr[data["姓名"]] = {"學號": data["學號"], "分數": 0}
})
.on("end", () => {
score_ob = JSON.parse(score)
score_ob2 = score_ob["members"]
score_ob2.forEach((e, i, a) => {
fullname = e["full_name"]
fullname = fullname.replace(" ", "")
if (arr[fullname]) {
arr[fullname]["分數"] = e["progress"]
} else {
console.log(fullname)
}
})
Object.keys(arr).forEach((e, i, a) => {
var it = {"學號": arr[e]["學號"], "姓名": e, "分數": arr[e]["分數"]}
wa.push(it)
})
var ws = fs.createWriteStream("1know_score.csv")
csv.write(wa, {headers: true}).pipe(ws)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment