上传文件至 /
This commit is contained in:
commit
5aaf7d4396
22
判断.py
Normal file
22
判断.py
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
import json
|
||||||
|
|
||||||
|
# 读取 CSV,去掉空白列
|
||||||
|
df = pd.read_csv(file_path, usecols=["题干(必填)", "正确答案\n(必填)"])
|
||||||
|
|
||||||
|
# 定义映射:判断题“正确”/“错误” -> A / B
|
||||||
|
ans_map = {"正确": "A", "错误": "B"}
|
||||||
|
|
||||||
|
# 构造结果列表
|
||||||
|
result = []
|
||||||
|
for _, row in df.iterrows():
|
||||||
|
q = str(row["题干(必填)"]).strip()
|
||||||
|
ans_text = str(row["正确答案\n(必填)"]).strip()
|
||||||
|
ans = ans_map.get(ans_text, ans_text) # 如果不是判断题格式,就原样保留
|
||||||
|
a = ["A.正确", "B.错误"]
|
||||||
|
result.append({"q": q, "a": a, "ans": ans})
|
||||||
|
|
||||||
|
# 保存为一行一个 JSON
|
||||||
|
output_path = "/mnt/data/questions.json"
|
||||||
|
with open(output_path, "w", encoding="utf-8") as f:
|
||||||
|
for item in result:
|
||||||
|
f.write(json.dumps(item, ensure_ascii=False) + "\n")
|
68
选择.py
Normal file
68
选择.py
Normal file
@ -0,0 +1,68 @@
|
|||||||
|
import csv
|
||||||
|
import json
|
||||||
|
|
||||||
|
def convert_csv_to_json(input_csv_path, output_json_path):
|
||||||
|
"""
|
||||||
|
将指定的CSV文件转换为每行一个JSON对象的格式。
|
||||||
|
|
||||||
|
Args:
|
||||||
|
input_csv_path (str): 输入CSV文件的路径。
|
||||||
|
output_json_path (str): 输出JSON文件的路径。
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
with open(input_csv_path, 'r', encoding='utf-8') as csv_file, \
|
||||||
|
open(output_json_path, 'w', encoding='utf-8') as json_file:
|
||||||
|
|
||||||
|
# 使用csv.reader读取文件,csv.DictReader也可以,但索引更直接
|
||||||
|
csv_reader = csv.reader(csv_file)
|
||||||
|
|
||||||
|
# 跳过表头
|
||||||
|
header = next(csv_reader)
|
||||||
|
|
||||||
|
# 选项字母列表
|
||||||
|
option_letters = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H']
|
||||||
|
|
||||||
|
# 遍历每一行数据
|
||||||
|
for row in csv_reader:
|
||||||
|
# 检查行是否为空或无效
|
||||||
|
if not row or len(row) < 10:
|
||||||
|
continue
|
||||||
|
|
||||||
|
# 提取问题(第1列,索引0)
|
||||||
|
question = row[0]
|
||||||
|
|
||||||
|
# 提取正确答案(第10列,索引9)
|
||||||
|
correct_answer = row[9]
|
||||||
|
|
||||||
|
# 提取选项(从第2列到第9列,索引1到8)
|
||||||
|
options = []
|
||||||
|
for i in range(len(option_letters)):
|
||||||
|
option_text = row[i+1].strip()
|
||||||
|
if option_text:
|
||||||
|
# 格式化选项为 "A. 文本"
|
||||||
|
options.append(f"{option_letters[i]}.{option_text}")
|
||||||
|
|
||||||
|
# 创建JSON对象
|
||||||
|
json_object = {
|
||||||
|
"q": question,
|
||||||
|
"a": options,
|
||||||
|
"ans": correct_answer
|
||||||
|
}
|
||||||
|
|
||||||
|
# 将JSON对象写入文件,每行一个对象
|
||||||
|
json_file.write(json.dumps(json_object, ensure_ascii=False) + '\n')
|
||||||
|
|
||||||
|
print(f"转换成功!文件已保存到:{output_json_path}")
|
||||||
|
|
||||||
|
except FileNotFoundError:
|
||||||
|
print(f"错误:找不到文件 {input_csv_path}")
|
||||||
|
except Exception as e:
|
||||||
|
print(f"发生错误:{e}")
|
||||||
|
|
||||||
|
# 请将'单选题.csv'替换为您的实际文件路径
|
||||||
|
input_file = '多选题.csv'
|
||||||
|
output_file = 'output.json'
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
convert_csv_to_json(input_file, output_file)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user