Python 如何将文本文件转换为json文件?

Python 如何将文本文件转换为json文件?,python,json,Python,Json,我是python新手,我想将文本文件转换为json文件。 下面是它的样子: #Q Three of these animals hibernate. Which one does not? ^ Sloth A Mouse B Sloth C Frog D Snake #Q What is the literal translation of the Greek word Embioptera, which denotes an order of insects, also known as w

我是python新手,我想将文本文件转换为json文件。 下面是它的样子:

#Q Three of these animals hibernate. Which one does not?
^ Sloth
A Mouse
B Sloth
C Frog
D Snake

#Q What is the literal translation of the Greek word Embioptera, which denotes an order of insects, also known as webspinners?
^ Lively wings
A Small wings
B None of these
C Yarn knitter
D Lively wings

#Q There is a separate species of scorpions which have two tails, with a venomous sting on each tail.
^ False
A True
B False

Contd
.
.
.
.
^
表示答案

我希望它是json格式的,如下所示。 例如:

我遇到过一些类似的帖子,下面是我尝试过的:

dataset = "file.txt"
data = []
with open(dataset) as ds:
    for line in ds:
        line = line.strip().split(",")
        print(line)
其输出为:

['']
['#Q What part of their body do the insects from order Archaeognatha use to spring up into the air?']
['^ Tail']
['A Antennae']
['B Front legs']
['C Hind legs']
['D Tail']
['']
['#Q What is the literal translation of the Greek word Embioptera', ' which denotes an order of insects', ' also known as webspinners?']
['^ Lively wings']
['A Small wings']
['B None of these']
['C Yarn knitter']
['D Lively wings']
['']

Contd.... 
包含逗号的句子由python列表分隔。我尝试使用.join,但没有得到预期的结果。

请让我知道如何处理这个问题。

我不是一次处理一行,而是使用正则表达式模式

dataset = "text.txt"
question_bank = []

with open(dataset) as ds:
    for i, line in enumerate(ds):
        line = line.strip("\n")
        if len(line) == 0:
            question_bank.append(question)
            question = {}
        elif line.startswith("#Q"):
            question = {"question": line}
        elif line.startswith("^"):
            question['answer'] = line.split(" ")[1]
        else:
            key, val = line.split(" ", 1)
            question[key] = val
    question_bank.append(question)

print({"questionBank":question_bank})

#for storing json file to local directory
final_output = {"questionBank":question_bank}

with open("output.json", "w") as outfile:
    outfile.write(json.dumps(final_output, indent=4))
这也更可靠,因为如果输入数据的格式不好,它会出错,而不是默默地忽略缺少字段的分组

PATTERN=r”“”[#]Q(?P.+)\n\^(?P.阅读更多有关


我忽略了阅读文本和编写JSON文件。

你不需要用逗号分隔,你需要查看每行的第一个字符,看看如何处理它…这就是我希望首先做的。我想运行一个循环,然后将行附加到列表和单独的
#q
^
…以便我可以n另一个循环来创建字典,并将其转储到json中。我希望是否有另一个有效的解决方案。
file_contents=[qna.splitlines()for qna in fp.read().split('\n\n')]
。这将把每个问题放在一个单独的列表中。
枚举
是不需要的,处理
拆分
的答案需要一个限制(或者最好使用
行[2:]
)。
x,y=z.split(“,1)
的一个好选项是
x,u,y=z.partition(“
)。
dataset = "text.txt"
question_bank = []

with open(dataset) as ds:
    for i, line in enumerate(ds):
        line = line.strip("\n")
        if len(line) == 0:
            question_bank.append(question)
            question = {}
        elif line.startswith("#Q"):
            question = {"question": line}
        elif line.startswith("^"):
            question['answer'] = line.split(" ")[1]
        else:
            key, val = line.split(" ", 1)
            question[key] = val
    question_bank.append(question)

print({"questionBank":question_bank})

#for storing json file to local directory
final_output = {"questionBank":question_bank}

with open("output.json", "w") as outfile:
    outfile.write(json.dumps(final_output, indent=4))