| import pandas as pd |
| import tempfile |
| import os |
|
|
| |
| |
| def create_choice_id(question_id, num): |
| return str(question_id) + 'CH00' + str(num) |
|
|
| |
| def create_correct_id(question_id, num): |
| return str(question_id) + 'A00' + str(num) |
|
|
| |
| def convert_last_fmt_question(csv_file): |
| input = pd.read_csv(csv_file.name) |
| |
| |
| output_question = pd.DataFrame({'問題ID': [], '知識ID': [], '出題形式ID': [], 'リード文':[],'問題': [ |
| ], '問題_翻訳': [], '正解文': [], '品詞': [], '解説テキスト': [],'リスト表示':[]}) |
| |
| |
| for index, row in input.iterrows(): |
| new_row = pd.DataFrame({'問題ID': [row['問題ID']], '知識ID': [row['知識ID']], '出題形式ID': [row['出題形式ID']], 'リード文':[row['リード文']],'問題': [ |
| row['問題']], '問題_翻訳': [row["問題_翻訳"]], '正解文': [row['正解文']], '品詞': [""], '解説テキスト': [row["解説テキスト"]],"リスト表示":[row["リスト表示"]]}) |
| output_question = pd.concat([output_question, new_row], ignore_index=True) |
|
|
| |
| |
| output_question.sort_values(by='問題ID', ascending=True, inplace=True) |
|
|
| |
| with tempfile.NamedTemporaryFile(delete=False, suffix='.csv') as tmp: |
| output_question.to_csv(tmp.name, index=False, encoding='cp932', errors='ignore') |
| output_path = tmp.name |
| |
| |
| new_path = os.path.join(os.path.dirname(output_path), "question.csv") |
| os.rename(output_path, new_path) |
| return new_path |
| def convert_last_fmt_selection(csv_file): |
| input = pd.read_csv(csv_file.name) |
| |
| output_choice = pd.DataFrame({'選択肢ID': [], '問題ID': [], '選択肢': [], '表示順': []}) |
| |
| for index, row in input.iterrows(): |
| |
| selection = row["選択肢"].split("/") |
| |
| for i in range(len(selection)): |
| new_row = pd.DataFrame({'選択肢ID': [create_choice_id( |
| row['問題ID'], i+1)], '問題ID': [row['問題ID']], '選択肢': [selection[i]], '表示順': [i+1]}) |
| output_choice = pd.concat([output_choice, new_row], ignore_index=True) |
| |
| |
| output_choice.sort_values(by='選択肢ID', ascending=True, inplace=True) |
| |
| with tempfile.NamedTemporaryFile(delete=False, suffix='.csv') as tmp: |
| output_choice.to_csv(tmp.name, index=False, encoding='cp932', errors='ignore') |
| output_path = tmp.name |
| |
| |
| new_path = os.path.join(os.path.dirname(output_path), "choices.csv") |
| os.rename(output_path, new_path) |
| return new_path |
| def convert_last_fmt_correct(csv_file): |
| input = pd.read_csv(csv_file.name) |
| |
| output_correct_choice = pd.DataFrame( |
| {'正解ID': [], '問題ID': [], '正解': [], '並び順': []}) |
| |
| for index, row in input.iterrows(): |
| |
| correct = row["正解"].split("/") |
| |
| for i in range(len(correct)): |
| new_row = pd.DataFrame({'正解ID': [create_correct_id( |
| row['問題ID'], i+1)], '問題ID': [row['問題ID']], '正解': [correct[i]], '並び順': [i+1]}) |
| output_correct_choice = pd.concat( |
| [output_correct_choice, new_row], ignore_index=True) |
|
|
| |
| output_correct_choice.sort_values(by='正解ID', ascending=True, inplace=True) |
| |
| with tempfile.NamedTemporaryFile(delete=False, suffix='.csv') as tmp: |
| output_correct_choice.to_csv(tmp.name, index=False, encoding='cp932', errors='ignore') |
| output_path = tmp.name |
| |
| |
| new_path = os.path.join(os.path.dirname(output_path), "correct.csv") |
| os.rename(output_path, new_path) |
| return new_path |