|
发表于 2023-4-23 11:50:53
|
显示全部楼层
本帖最后由 isdkz 于 2023-4-23 11:54 编辑
不明白你说什么
不过我有自己的想法,这种半结构化的数据可以用谷歌开源的 textfsm 库来解决
使用之前得先安装:
参考代码:
- import textfsm
- data = """
- 1、( )的征缴按照《社会保险费征缴暂行条例》关于基本养老保险费、基本医疗保险费、失业保险费的征缴规定执行。
- A.工伤保险费
- B.意外保险费
- C.医疗保险费
- D.人身保险费
- 答案:A
- 2、( )对建筑工程安全防护、文明施工措施费用的使用负总责。
- A.分包单位
- B.转包单位
- C.发包单位
- D.工程总承包单位
- 答案:D
- """
- template = """Value Number (\d+)
- Value Question (.+)
- Value List Option ([A-D]\..+)
- Value Answer ([A-D])
- Start
- ^${Number}、${Question}
- ^${Option}
- ^答案:${Answer} -> Continue.Record
- ^\s* -> Record
- """
- with open("template.textfsm", "w") as f:
- f.write(template)
- with open("template.textfsm") as f:
- fsm = textfsm.TextFSM(f)
- result = fsm.ParseText(data)
- for item in result:
- number, question, options, answer = item
- print(f"问题 {number}: {question}")
- print("选项:")
- for option in options:
- print(option)
- print(f"答案: {answer}")
- print()
复制代码
|
评分
-
查看全部评分
|