替换json为yml加载,简化了编辑。同时优化了数据结构

This commit is contained in:
mxr612 2025-03-05 10:08:57 +00:00
parent 819e773342
commit 9356c13339
10 changed files with 103 additions and 886 deletions

37
app.py
View File

@ -3,7 +3,7 @@ from fastapi.responses import HTMLResponse
import markdown import markdown
from fastapi.staticfiles import StaticFiles from fastapi.staticfiles import StaticFiles
from fastapi.templating import Jinja2Templates from fastapi.templating import Jinja2Templates
import json import json, yaml
import os import os
import uvicorn import uvicorn
@ -16,11 +16,10 @@ def load_all_scales():
scale_folder = 'scales' scale_folder = 'scales'
scales = {} scales = {}
for filename in os.listdir(scale_folder): for filename in os.listdir(scale_folder):
if filename.endswith('.json'): if filename.endswith(('.yaml', '.yml')):
with open(os.path.join(scale_folder, filename), 'r', encoding='utf-8') as f: with open(os.path.join(scale_folder, filename), 'r', encoding='utf-8') as f:
scale = json.load(f) scale = yaml.safe_load(f)
# 使用文件名作为问卷的唯一标识 scale_id = os.path.splitext(filename)[0] # 使用文件名作为标识
scale_id = os.path.splitext(filename)[0]
scales[scale_id] = scale scales[scale_id] = scale
return scales return scales
@ -54,33 +53,27 @@ async def scale(request: Request, scale_id: str):
@app.post("/result/{scale_id}", response_class=HTMLResponse) @app.post("/result/{scale_id}", response_class=HTMLResponse)
async def result(request: Request, scale_id: str): async def result(request: Request, scale_id: str):
# 保留原有的计分逻辑...
form_data = await request.form() form_data = await request.form()
# print(request.form)
scales = load_all_scales() scales = load_all_scales()
scale = scales.get(scale_id) scale = scales.get(scale_id)
if scale: if scale:
# 这里可以添加保存数据到数据库等逻辑
responses = {} responses = {}
ranges = {} ranges = {}
# print(scale['questions']) for subscale, qids in scale['subscales'].items():
for question in scale['questions']: responses[subscale] = 0
# print(question['subscale']) ranges[subscale] = [len(scale['range'][0]*qids),len(scale['range'][1]*qids)]
if question['subscale'] not in responses: for qid in qids:
responses[question['subscale']] = 0 print(qid)
ranges[question['subscale']] = [0,0] if qid<0:
if 'reverse' in question and question['reverse']: responses[subscale] += scale['range'][0] + scale['range'][1] - int(form_data[str(-qid)])
responses[question['subscale']] += question['range'][1] + question['range'][0] - int( form_data[question['id']]) else:
else: responses[subscale] += int(form_data[str(qid)])
responses[question['subscale']] += int( form_data[question['id']])
ranges[question['subscale']][0] += question['range'][0]
ranges[question['subscale']][1] += question['range'][1]
# 这里可以添加保存数据到数据库等逻辑
# print(ranges)
return templates.TemplateResponse("result.html", { return templates.TemplateResponse("result.html", {
"request": request, "request": request,
"responses": responses, "responses": responses,
"ranges": ranges, "ranges": ranges,
"scale_title": scale['title'] "scale": scale
}) })
raise HTTPException(status_code=404, detail="问卷未找到") raise HTTPException(status_code=404, detail="问卷未找到")

Binary file not shown.

View File

@ -1,374 +0,0 @@
{
"title": "中国大五人格问卷(简式版)",
"description": "",
"instructions": "下面是一些描述人们性格特点的句子请根据每个句子与您性格相符程度选择相应的数字。1-6分别代表完全不符合、大部分不符合、有点不符合、有点符合、大部分符合、完全符合。",
"questions": [
{
"id": "1",
"subscale": "神经质",
"text": "我常感到害怕",
"range": [
1,
6
]
},
{
"id": "2",
"subscale": "严谨性",
"text": "一旦确定了目标,我会坚持努力地实现它",
"range": [
1,
6
]
},
{
"id": "3",
"subscale": "宜人性",
"text": "我觉得大部分人基本上是心怀善意的",
"range": [
1,
6
]
},
{
"id": "4",
"subscale": "开放性",
"text": "我头脑中经常充满生动的画面",
"range": [
1,
6
]
},
{
"id": "5",
"subscale": "外向性",
"text": "我对人多的聚会感到乏味",
"range": [
1,
6
],
"reverse": 1
},
{
"id": "6",
"subscale": "神经质",
"text": "有时我觉得自己一无是处",
"range": [
1,
6
]
},
{
"id": "7",
"subscale": "严谨性",
"text": "我常常是仔细考虑之后才做出决定",
"range": [
1,
6
]
},
{
"id": "8",
"subscale": "宜人性",
"text": "我不太关心别人是否受到不公正的待遇",
"range": [
1,
6
],
"reverse": 1
},
{
"id": "9",
"subscale": "开放性",
"text": "我是个勇于冒险的人",
"range": [
1,
6
]
},
{
"id": "10",
"subscale": "外向性",
"text": "在热闹的聚会上,我常常表现主动并尽情玩耍",
"range": [
1,
6
]
},
{
"id": "11",
"subscale": "神经质",
"text": "别人一句漫不经心的话,我常会联系在自己身上",
"range": [
1,
6
]
},
{
"id": "12",
"subscale": "严谨性",
"text": "别人认为我是个慎重的人",
"range": [
1,
6
]
},
{
"id": "13",
"subscale": "宜人性",
"text": "我时常觉得别人的痛苦与我无关",
"range": [
1,
6
],
"reverse": 1
},
{
"id": "14",
"subscale": "开放性",
"text": "我喜欢冒险",
"range": [
1,
6
]
},
{
"id": "15",
"subscale": "外向性",
"text": "我尽量避免参加人多的聚会和嘈杂的环境",
"range": [
1,
6
],
"reverse": 1
},
{
"id": "16",
"subscale": "神经质",
"text": "在面对压力时,我有种快要崩溃的感觉",
"range": [
1,
6
]
},
{
"id": "17",
"subscale": "严谨性",
"text": "我喜欢一开头就把事情计划好",
"range": [
1,
6
]
},
{
"id": "18",
"subscale": "宜人性",
"text": "我是那种只照顾好自己,不替别人担忧的人",
"range": [
1,
6
],
"reverse": 1
},
{
"id": "19",
"subscale": "开放性",
"text": "我对许多事情有着很强的好奇心",
"range": [
1,
6
]
},
{
"id": "20",
"subscale": "外向性",
"text": "有我在的场合一般不会冷场",
"range": [
1,
6
]
},
{
"id": "21",
"subscale": "神经质",
"text": "我常担忧一些无关紧要的事情",
"range": [
1,
6
]
},
{
"id": "22",
"subscale": "严谨性",
"text": "我工作或学习很勤奋",
"range": [
1,
6
]
},
{
"id": "23",
"subscale": "宜人性",
"text": "虽然社会上有些骗子,但我觉得大部分人还是可信的",
"range": [
1,
6
]
},
{
"id": "24",
"subscale": "开放性",
"text": "我身上具有别人没有的冒险精神",
"range": [
1,
6
]
},
{
"id": "25",
"subscale": "外向性",
"text": "在一个团体中,我希望处于领导地位",
"range": [
1,
6
]
},
{
"id": "26",
"subscale": "神经质",
"text": "我常常感到内心不踏实",
"range": [
1,
6
]
},
{
"id": "27",
"subscale": "严谨性",
"text": "我是个倾尽全力做事的人",
"range": [
1,
6
]
},
{
"id": "28",
"subscale": "宜人性",
"text": "当别人想我诉说不幸的时,我常感到难过",
"range": [
1,
6
]
},
{
"id": "29",
"subscale": "开放性",
"text": "我渴望学习一些新东西,即使它们与我的日常生活无关",
"range": [
1,
6
]
},
{
"id": "30",
"subscale": "外向性",
"text": "别人多认为我是一个热情和友好的人",
"range": [
1,
6
]
},
{
"id": "31",
"subscale": "神经质",
"text": "我常担心有什么不好的事情发生",
"range": [
1,
6
]
},
{
"id": "32",
"subscale": "严谨性",
"text": "在工作上,我常只求能应付过去便可",
"range": [
1,
6
],
"reverse": 1
},
{
"id": "33",
"subscale": "宜人性",
"text": "尽管人类社会存在着一些阴暗的东西(如战争、罪恶、欺诈),我仍相信人性总的来说是善良的",
"range": [
1,
6
]
},
{
"id": "34",
"subscale": "开放性",
"text": "我的想象力相当丰富",
"range": [
1,
6
]
},
{
"id": "35",
"subscale": "外向性",
"text": "我喜欢参加社交与娱乐聚会",
"range": [
1,
6
]
},
{
"id": "36",
"subscale": "神经质",
"text": "我很少感到忧郁或沮丧",
"range": [
1,
6
],
"reverse": 1
},
{
"id": "37",
"subscale": "严谨性",
"text": "做事讲究逻辑和条理是我的一个特点",
"range": [
1,
6
]
},
{
"id": "38",
"subscale": "宜人性",
"text": "我常为那些遭遇不幸的人感到难过",
"range": [
1,
6
]
},
{
"id": "39",
"subscale": "开放性",
"text": "我很愿意也很容易接受那些新事物、新观点、新想法",
"range": [
1,
6
]
},
{
"id": "40",
"subscale": "外向性",
"text": "我希望成为领导者而不是被领导者",
"range": [
1,
6
]
}
]
}

51
scales/CBF-PI-B.yml Normal file
View File

@ -0,0 +1,51 @@
title: 中国大五人格问卷(简式版)
instruction: 下面是一些描述人们性格特点的句子请根据每个句子与您性格相符程度选择相应的数字。1-6分别代表完全不符合、大部分不符合、有点不符合、有点符合、大部分符合、完全符合。
description: ""
subscales:
神经质: [1,6,11,16,21,26,31,-36]
严谨性: [2,7,12,17,22,27,-32,37]
宜人性: [3,-8,-13,-18,23,28,33,38]
开放性: [4,9,14,19,24,29,34,39]
外向性: [-5,10,-15,20,25,30,35,40]
range: [1,6]
questions:
1: 我常感到害怕
2: 一旦确定了目标,我会坚持努力地实现它
3: 我觉得大部分人基本上是心怀善意的
4: 我头脑中经常充满生动的画面
5: 我对人多的聚会感到乏味
6: 有时我觉得自己一无是处
7: 我常常是仔细考虑之后才做出决定
8: 我不太关心别人是否受到不公正的待遇
9: 我是个勇于冒险的人
10: 在热闹的聚会上,我常常表现主动并尽情玩耍
11: 别人一句漫不经心的话,我常会联系在自己身上
12: 别人认为我是个慎重的人
13: 我时常觉得别人的痛苦与我无关
14: 我喜欢冒险
15: 我尽量避免参加人多的聚会和嘈杂的环境
16: 在面对压力时,我有种快要崩溃的感觉
17: 我喜欢一开头就把事情计划好
18: 我是那种只照顾好自己,不替别人担忧的人
19: 我对许多事情有着很强的好奇心
20: 有我在的场合一般不会冷场
21: 我常担忧一些无关紧要的事情
22: 我工作或学习很勤奋
23: 虽然社会上有些骗子,但我觉得大部分人还是可信的
24: 我身上具有别人没有的冒险精神
25: 在一个团体中,我希望处于领导地位
26: 我常常感到内心不踏实
27: 我是个倾尽全力做事的人
28: 当别人想我诉说不幸的时,我常感到难过
29: 我渴望学习一些新东西,即使它们与我的日常生活无关
30: 别人多认为我是一个热情和友好的人
31: 我常担心有什么不好的事情发生
32: 在工作上,我常只求能应付过去便可
33: 尽管人类社会存在着一些阴暗的东西(如战争、罪恶、欺诈),我仍相信人性总的来说是善良的
34: 我的想象力相当丰富
35: 我喜欢参加社交与娱乐聚会
36: 我很少感到忧郁或沮丧
37: 做事讲究逻辑和条理是我的一个特点
38: 我常为那些遭遇不幸的人感到难过
39: 我很愿意也很容易接受那些新事物、新观点、新想法
40: 我希望成为领导者而不是被领导者

View File

@ -0,0 +1,28 @@
title: 中庸实践思维量表
instruction:
description: 中庸实践思维量表
subscales:
恰如其分/不走极端: [-3,-6,-13,-15,-16,-17,-18]
内外和谐: [-2,-4,-7,-9,-11,-14]
阴阳转换: [1,5,8,10,12]
总分: [1,-2,-3,-4,5,-6,-7,8,-9,10,-11,12,-13,-14,-15,-16,-17,-18]
range: [0,3]
questions:
1: 我认为世界上任何事情的发展,都有其历史根源和背景
2: 做事情既要考虑他人,又要兼顾自己,这对我来说太难了
3: 只有成功的事情,对我才有好处
4: 我经常觉得自己与所在环境格格不入
5: 在一定的条件下,有利的方面也会向不利的方面转化
6: 失败的事情对我没有一点好处
7: 做事总是要考虑他人的话,往往只有委屈了自己
8: 我认为任何事物都有积极的一面,也有消极的一面
9: 心平气和地指出他人的错误,我觉得很难
10: 我认为任何事情做过了头,其结果反而会适得其反
11: 我经常觉得自己难以融入所在的生活或工作环境
12: 进与退、盛与衰等是事物在发展过程中的两个可能态势
13: 采用最强硬的处事方式,通常会达到最大效果
14: 我觉得我所做的很多事情都是不该做的
15: 我认为爱情要么是一百分,要么是零分
16: 衡量一个人如何,只要看他/她的工作或学业成绩就知道了
17: 我认为只要能够解决现在遇到的问题,就可以不择手段
18: 与人发生冲突时,为了争一口气,常常得豁出去

View File

@ -4,18 +4,18 @@
<head> <head>
<meta charset="UTF-8"> <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Result</title> <title>{{scale_title}}</title>
<link rel="stylesheet" href="/static/styles.css"> <link rel="stylesheet" href="/static/styles.css">
</head> </head>
<body> <body>
<h1>{{ scale_title }} </h1> <h1>{{ scale.title }} </h1>
<!-- <h2>您的结果如下:</h2> -->
<ul> <ul>
{% for key, value in responses.items() %} {% for key, value in responses.items() %}
<li>{{ key }}: 在 {{ranges[key][0]}} 到 {{ranges[key][1]}} 的量表中得分 {{ value }}</li> <li>{{ key }}: 在 {{ranges[key][0]}} 到 {{ranges[key][1]}} 的量表中得分 {{ value }}</li>
{% endfor %} {% endfor %}
</ul> </ul>
<p>{{scale.description}}</p>
</body> </body>
</html> </html>

View File

@ -12,14 +12,13 @@
<h1>{{ scale.title }}</h1> <h1>{{ scale.title }}</h1>
<p>{{ scale.instructions }}</p> <p>{{ scale.instructions }}</p>
<form action="/result/{{ scale_id }}" method="post"> <form action="/result/{{ scale_id }}" method="post">
{% for question in scale.questions %} {% for id, question in scale.questions.items() %}
<label for="{{ question.id }}">{{ question.text }}</label> <label for="{{ id }}">{{ id }}. {{ question }}</label>
<div class="scale-button"> <div class="scale-button">
<!-- 将下拉菜单替换为单选按钮 --> {% for option in range(scale.range[0], scale.range[1]+1) %}
{% for option in range(question.range[0], question.range[1]+1) %} <input type="radio" id="{{ id }}_{{ option }}" name="{{ id }}" value="{{ option }}" {% if not ( 'optional'
<input type="radio" id="{{ question.id }}_{{ option }}" name="{{ question.id }}" value="{{ option }}" {% if in question and question.optional) %}required{% endif %}>
not ( 'optional' in question and question.optional) %}required{% endif %}> <label for="{{ id }}_{{ option }}">{{ option }}</label>
<label for="{{ question.id }}_{{ option }}">{{ option }}</label>
{% endfor %} {% endfor %}
</div> </div>
{% endfor %} {% endfor %}

View File

@ -1,46 +0,0 @@
我常感到害怕
一旦确定了目标,我会坚持努力地实现它
我觉得大部分人基本上是心怀善意的
我头脑中经常充满生动的画面
-我对人多的聚会感到乏味
有时我觉得自己一无是处
我常常是仔细考虑之后才做出决定
-我不太关心别人是否受到不公正的待遇
我是个勇于冒险的人
在热闹的聚会上,我常常表现主动并尽情玩耍
别人一句漫不经心的话,我常会联系在自己身上
别人认为我是个慎重的人
-我时常觉得别人的痛苦与我无关
我喜欢冒险
-我尽量避免参加人多的聚会和嘈杂的环境
在面对压力时,我有种快要崩溃的感觉
我喜欢一开头就把事情计划好
-我是那种只照顾好自己,不替别人担忧的人
我对许多事情有着很强的好奇心
有我在的场合一般不会冷场
我常担忧一些无关紧要的事情
我工作或学习很勤奋
虽然社会上有些骗子,但我觉得大部分人还是可信的
我身上具有别人没有的冒险精神
在一个团体中,我希望处于领导地位
我常常感到内心不踏实
我是个倾尽全力做事的人
当别人想我诉说不幸的时,我常感到难过
我渴望学习一些新东西,即使它们与我的日常生活无关
别人多认为我是一个热情和友好的人
我常担心有什么不好的事情发生
-在工作上,我常只求能应付过去便可
尽管人类社会存在着一些阴暗的东西(如战争、罪恶、欺诈),我仍相信人性总的来说是善良的
我的想象力相当丰富
我喜欢参加社交与娱乐聚会
-我很少感到忧郁或沮丧
做事讲究逻辑和条理是我的一个特点
我常为那些遭遇不幸的人感到难过
我很愿意也很容易接受那些新事物、新观点、新想法
我希望成为领导者而不是被领导者
--
神经质=1,6,11,16,21,26,31,36
严谨性=2,7,12,17,22,27,32,37
宜人性=3,8,13,18,23,28,33,38
开放性=4,9,14,19,24,29,34,39
外向性=5,10,15,20,25,30,35,40

View File

@ -1,374 +0,0 @@
{
"title": "",
"description": "",
"instructions": "",
"questions": [
{
"id": "1",
"subscale": "神经质",
"text": "我常感到害怕",
"range": [
1,
6
]
},
{
"id": "2",
"subscale": "严谨性",
"text": "一旦确定了目标,我会坚持努力地实现它",
"range": [
1,
6
]
},
{
"id": "3",
"subscale": "宜人性",
"text": "我觉得大部分人基本上是心怀善意的",
"range": [
1,
6
]
},
{
"id": "4",
"subscale": "开放性",
"text": "我头脑中经常充满生动的画面",
"range": [
1,
6
]
},
{
"id": "5",
"subscale": "外向性",
"text": "我对人多的聚会感到乏味",
"range": [
1,
6
],
"reverse": 1
},
{
"id": "6",
"subscale": "神经质",
"text": "有时我觉得自己一无是处",
"range": [
1,
6
]
},
{
"id": "7",
"subscale": "严谨性",
"text": "我常常是仔细考虑之后才做出决定",
"range": [
1,
6
]
},
{
"id": "8",
"subscale": "宜人性",
"text": "我不太关心别人是否受到不公正的待遇",
"range": [
1,
6
],
"reverse": 1
},
{
"id": "9",
"subscale": "开放性",
"text": "我是个勇于冒险的人",
"range": [
1,
6
]
},
{
"id": "10",
"subscale": "外向性",
"text": "在热闹的聚会上,我常常表现主动并尽情玩耍",
"range": [
1,
6
]
},
{
"id": "11",
"subscale": "神经质",
"text": "别人一句漫不经心的话,我常会联系在自己身上",
"range": [
1,
6
]
},
{
"id": "12",
"subscale": "严谨性",
"text": "别人认为我是个慎重的人",
"range": [
1,
6
]
},
{
"id": "13",
"subscale": "宜人性",
"text": "我时常觉得别人的痛苦与我无关",
"range": [
1,
6
],
"reverse": 1
},
{
"id": "14",
"subscale": "开放性",
"text": "我喜欢冒险",
"range": [
1,
6
]
},
{
"id": "15",
"subscale": "外向性",
"text": "我尽量避免参加人多的聚会和嘈杂的环境",
"range": [
1,
6
],
"reverse": 1
},
{
"id": "16",
"subscale": "神经质",
"text": "在面对压力时,我有种快要崩溃的感觉",
"range": [
1,
6
]
},
{
"id": "17",
"subscale": "严谨性",
"text": "我喜欢一开头就把事情计划好",
"range": [
1,
6
]
},
{
"id": "18",
"subscale": "宜人性",
"text": "我是那种只照顾好自己,不替别人担忧的人",
"range": [
1,
6
],
"reverse": 1
},
{
"id": "19",
"subscale": "开放性",
"text": "我对许多事情有着很强的好奇心",
"range": [
1,
6
]
},
{
"id": "20",
"subscale": "外向性",
"text": "有我在的场合一般不会冷场",
"range": [
1,
6
]
},
{
"id": "21",
"subscale": "神经质",
"text": "我常担忧一些无关紧要的事情",
"range": [
1,
6
]
},
{
"id": "22",
"subscale": "严谨性",
"text": "我工作或学习很勤奋",
"range": [
1,
6
]
},
{
"id": "23",
"subscale": "宜人性",
"text": "虽然社会上有些骗子,但我觉得大部分人还是可信的",
"range": [
1,
6
]
},
{
"id": "24",
"subscale": "开放性",
"text": "我身上具有别人没有的冒险精神",
"range": [
1,
6
]
},
{
"id": "25",
"subscale": "外向性",
"text": "在一个团体中,我希望处于领导地位",
"range": [
1,
6
]
},
{
"id": "26",
"subscale": "神经质",
"text": "我常常感到内心不踏实",
"range": [
1,
6
]
},
{
"id": "27",
"subscale": "严谨性",
"text": "我是个倾尽全力做事的人",
"range": [
1,
6
]
},
{
"id": "28",
"subscale": "宜人性",
"text": "当别人想我诉说不幸的时,我常感到难过",
"range": [
1,
6
]
},
{
"id": "29",
"subscale": "开放性",
"text": "我渴望学习一些新东西,即使它们与我的日常生活无关",
"range": [
1,
6
]
},
{
"id": "30",
"subscale": "外向性",
"text": "别人多认为我是一个热情和友好的人",
"range": [
1,
6
]
},
{
"id": "31",
"subscale": "神经质",
"text": "我常担心有什么不好的事情发生",
"range": [
1,
6
]
},
{
"id": "32",
"subscale": "严谨性",
"text": "在工作上,我常只求能应付过去便可",
"range": [
1,
6
],
"reverse": 1
},
{
"id": "33",
"subscale": "宜人性",
"text": "尽管人类社会存在着一些阴暗的东西(如战争、罪恶、欺诈),我仍相信人性总的来说是善良的",
"range": [
1,
6
]
},
{
"id": "34",
"subscale": "开放性",
"text": "我的想象力相当丰富",
"range": [
1,
6
]
},
{
"id": "35",
"subscale": "外向性",
"text": "我喜欢参加社交与娱乐聚会",
"range": [
1,
6
]
},
{
"id": "36",
"subscale": "神经质",
"text": "我很少感到忧郁或沮丧",
"range": [
1,
6
],
"reverse": 1
},
{
"id": "37",
"subscale": "严谨性",
"text": "做事讲究逻辑和条理是我的一个特点",
"range": [
1,
6
]
},
{
"id": "38",
"subscale": "宜人性",
"text": "我常为那些遭遇不幸的人感到难过",
"range": [
1,
6
]
},
{
"id": "39",
"subscale": "开放性",
"text": "我很愿意也很容易接受那些新事物、新观点、新想法",
"range": [
1,
6
]
},
{
"id": "40",
"subscale": "外向性",
"text": "我希望成为领导者而不是被领导者",
"range": [
1,
6
]
}
]
}

View File

@ -1,60 +0,0 @@
import json
def txt_to_json(input_file, output_file):
# 获取用户输入的range范围
range_input = input("请输入评分范围(格式:最小值,最大值例如1,5: ")
range_values = list(map(int, range_input.split(',')))
# 读取并分割问题和指令
with open(input_file, 'r', encoding='utf-8') as f:
all_lines = [line.strip() for line in f if line.strip()]
# 分割问题定义和subscale指令
if '--' in all_lines:
split_index = all_lines.index('--')
question_lines = all_lines[:split_index]
subscale_lines = all_lines[split_index+1:]
else:
question_lines = all_lines
subscale_lines = []
# 解析subscale指令
subscale_map = {}
for line in subscale_lines:
if '=' in line:
name, ids = line.split('=', 1)
for q_id in ids.split(','):
subscale_map[q_id.strip()] = name.strip()
questions = []
for idx, line in enumerate(question_lines, 1):
# 处理前导"-"标记
reverse = 1 if line.startswith('-') else None
text = line[1:] if reverse else line
question = {
"id": str(idx),
"subscale": subscale_map.get(str(idx), ""), # 新增subscale映射
"text": text,
"range": range_values.copy()
}
if reverse:
question["reverse"] = 1
questions.append(question)
# 构建完整JSON结构
result = {
"title": "",
"description": "",
"instructions": "",
"questions": questions
}
# 写入输出文件
with open(output_file, 'w', encoding='utf-8') as f:
json.dump(result, f, ensure_ascii=False, indent=4)
if __name__ == "__main__":
txt_to_json("input.txt", "output.json")