吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 2033|回复: 23
收起左侧

[经验求助] 关于Pycharm,TXT文本转换JS

[复制链接]
AkaTerrorist 发表于 2022-6-13 22:37
25吾爱币
研究了两天。。。麻烦好心大哥,谢谢!

【run显示:error: the following arguments are required: rawfile, output】,


---------------------------------------------------------------------------------------------------------------------------------------------------

# -*- coding: utf-8 -*-
import sys, re, io, json

questionreobj_withanswer = re.compile('\s*(\d+)[\.,,。\s]+(.+?)(?:<picture\s*src=[\'"]?(.+?)[\'"]?\s*>)?\s*(A[\.,,。\s]+.+)(?=[A-Z])([A-Z])\s*$')
questionreobj_withoutanswer = re.compile('\s*(\d+)[\.,,。\s]+(.+?)(?:<picture\s*src=[\'"]?(.+?)[\'"]?\s*>)?\s*(A[\.,,。\s]+.+)$')
optionsreobj = re.compile('([A-Z])[.,,。\s]+(.+?)(?:\s*)(?:(?=[A-Z][\.,,。\s]+)|$)')
answerreobj = re.compile('[A-Z]')

startofquestion = re.compile('^\s*(\d+)[\.,,。\s]*')
def main(filename, output, answer_sheet=None):
        if answer_sheet:
                questionreobj = questionreobj_withoutanswer
                with io.open(answer_sheet) as f:
                        lines = f.readlines()
                        answer_string = ' '.join(lines)
                        answers = answerreobj.findall(answer_string)
        else:
                questionreobj = questionreobj_withanswer
        questions = []

        idx = 0
        with io.open(filename) as f:
                answer = None
                if answer_sheet:
                        answer = answers[idx]

                buffer = ''
                for line in f:
                        if startofquestion.match(line):
                                if buffer:
                                        if answer_sheet:
                                                answer = answers[idx]
                                        question = parse(questionreobj, buffer, answer)
                                        idx+=1
                                        if question:
                                                questions.append(question)
                                buffer = ''
                        buffer+=line.strip()
        if buffer:
                last_question = parse(questionreobj, buffer, answer)
                questions.append(last_question)

        json_string = json.dumps(questions, ensure_ascii=False)
        js = 'let questions = ' + json_string + '\nmodule.exports = questions'
        with io.open(output, 'w') as f:
                f.write(js)


def parse(reobj, string, answer=None):
        matchofquestion = reobj.match(string)
        if not matchofquestion:
                return False
        grps = matchofquestion.groups()
        if len(grps)==5:
                no, stem, img, optionstring, answer = grps
        else:
                no, stem, img, optionstring = grps

        matchofoptions = optionsreobj.findall(optionstring)
        if not matchofoptions:
                return False
        questionobj = dict(no=int(no), stem=stem, img=img, options=[], answer=answer)
        for option, content in matchofoptions:
                opt = dict(option=option, content=content)
                questionobj['options'].append(opt)
        return questionobj

if __name__=='__main__':
        import argparse
        parser = argparse.ArgumentParser()
        parser.add_argument("rawfile", help="text file contains questions")
        parser.add_argument("output", help="output filename")
        parser.add_argument("-a", "--answer_sheet", help="add a answer sheet if the answers are not included in the text question file")
        args = parser.parse_args()

        main(args.rawfile, args.output, args.answer_sheet)
        print("Convert Finished. Please copy the file {} to desired location.".format(args.output))

最佳答案

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

封心棒棒糖 发表于 2022-6-13 22:37
这个不难吧
 楼主| AkaTerrorist 发表于 2022-6-13 22:46
1.以下说法不正确的是____。
A.领陆指国家主权管辖下的陆地及其底土
B.领空指国家主权管辖下的全部空域及其底土
C.
D.

----------------------------------------------------转换后----------------------------------------------------------

let questions = [{"answer": "B", "stem": "以下说法不正确的是____。", "options": [{"content": "领陆指国家主权管辖下的陆地及其底土", "option": "A"}, {"content": "领空指国家主权管辖下的全部空域及其底土", "option": "B"}], "img": null, "no": 1},
 楼主| AkaTerrorist 发表于 2022-6-14 15:12

大哥帮帮忙哇,,file:///C:/Users/1/Desktop/%E5%B1%8F%E5%B9%95%E6%88%AA%E5%9B%BE%202022-06-14%20151150.png
笔墨纸砚 发表于 2022-6-14 16:36
真不难  利用正则即可
封心棒棒糖 发表于 2022-6-14 16:45

看不到啊
 楼主| AkaTerrorist 发表于 2022-6-14 16:58

py脚本贴在上面了,(脚本应该没有问题吧,就是一些参数需要设置吧)实例文档用那一题就可以了,我就想请问大哥该怎么运转起来,网上查了相关资料,什么指定参数,相对引用..请问我还需要提供什么呢
封心棒棒糖 发表于 2022-6-14 17:49
AkaTerrorist 发表于 2022-6-14 16:58
py脚本贴在上面了,(脚本应该没有问题吧,就是一些参数需要设置吧)实例文档用那一题就可以了,我就想请 ...

那个脚本并不能看出来什么东西
 楼主| AkaTerrorist 发表于 2022-6-14 20:49
封心棒棒糖 发表于 2022-6-14 17:49
那个脚本并不能看出来什么东西

我就一个py脚本,一个txt文件。。
[img=1920,1042]https://img1.imgtp.com/2022/06/14/4I0Zy3uZ.png
https://img1.imgtp.com/2022/06/14/Hyq47Vvm.png
封心棒棒糖 发表于 2022-6-14 22:06
AkaTerrorist 发表于 2022-6-14 20:49
我就一个py脚本,一个txt文件。。
https://img1.imgtp.com/2022/06/14/4I0Zy3uZ.png
h ...

对,重要的就是那个txt文件
返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - 52pojie.cn ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2026-3-11 07:02

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表