gpt4 book ai didi

python - 如何合并列表中的损坏文本并附加到字典中?

转载 作者:行者123 更新时间:2023-12-01 09:09:02 24 4
gpt4 key购买 nike

引用Python module for converting PDF to text发布后,pdf 文件被抓取并提取数据。在抓取时,数据被分成两个独立的变量。如何合并这些数据并将其提取为字典?
例如。

content = ['Sample Questions Set 1 ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '01  Which function among the following can’t be accessed outside ', 'the class in java in same package? ', 'A. public void show()。 ', 'B. void show()。 ', 'C. protected show()。 ', 'D. static void show()。 ', '02  How many private member functions are allowed in a class ? ', 'A. Only 1 ', 'B. Only 7 ', 'C. Only 255 ', 'D. As many as required ', '03  Can main() function be made private? ', 'A. Yes, always。 ', 'B. Yes, if program doesn’t contain any classes。 ', 'C. No, because main function is user defined。 ', 'D. No, never。 ', '04  If private member functions are to be declared in C++ then_________。 ', 'A. private:  ', 'B. private ', 'C. private(private member list) ', 'D. private :- <private members> ', '05  If a function in java is declared private then it _________。 ', 'A. Can’t access the standard output ', 'B. Can access the standard output。 ', 'C. Can’t access any output stream。 ', 'D. Can access only the output streams。 ']

输出:

questions = [{'Qid':01,'Qtext':'Which function among the following can’t be accessed outside the class in java in same package?','A.':'public void show()。','B.':' void show()。','C.':'protected show()。','D.':'static void show()'},{'Qid':02,....},{...},{...},{...}]

最佳答案

执行以下操作:

questions = []
for s in content:
s = s.lstrip()
if s:
if s[0].isdigit():
questions.append({'Qid': len(questions) + 1, 'Qtext': s.split(maxsplit=1)[1]})
elif s[0].isalpha() and s[1] == '.':
questions[-1][s[:2]] = s.split(maxsplit=1)[1]
elif questions:
questions[-1]['Qtext'] += s

问题将变为:

[{'Qid': 1, 'Qtext': 'Which function among the following can’t be accessed outside the class in java in same package? ', 'A.': 'public void show()。 ', 'B.': 'void show()。 ', 'C.': 'protected show()。 ', 'D.': 'static void show()。 '}, {'Qid': 2, 'Qtext': 'How many private member functions are allowed in a class ? ', 'A.': 'Only 1 ', 'B.': 'Only 7 ', 'C.': 'Only 255 ', 'D.': 'As many as required '}, {'Qid': 3, 'Qtext': 'Can main() function be made private? ', 'A.': 'Yes, always。 ', 'B.': 'Yes, if program doesn’t contain any classes。 ', 'C.': 'No, because main function is user defined。 ', 'D.': 'No, never。 '}, {'Qid': 4, 'Qtext': 'If private member functions are to be declared in C++ then_________。 ', 'A.': 'private:  ', 'B.': 'private ', 'C.': 'private(private member list) ', 'D.': 'private :- <private members> '}, {'Qid': 5, 'Qtext': 'If a function in java is declared private then it _________。 ', 'A.': 'Can’t access the standard output ', 'B.': 'Can access the standard output。 ', 'C.': 'Can’t access any output stream。 ', 'D.': 'Can access only the output streams。 '}]

关于python - 如何合并列表中的损坏文本并附加到字典中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51816688/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com