在Python中将具有dictionarylike表示的字符串转换为dictionary

2024-10-03 09:11:44 发布

您现在位置:Python中文网/ 问答频道 /正文

假设我有一个字符串,看起来是这样的:

text = '''
{"question":"In 2017, what was the approximate number of clinics in the US that provided abortion services?","category":"RFB","answers":["80","800","8000","80000"],"sources":["https://www.guttmacher.org/fact-sheet/induced-abortion-united-states"]} 
{"question":"Compared to actively religious US adults, how many unaffiliated US adults were active in non-religious voluntary organizations, such as charities?","category":"DFB","answers":["Slightly fewer (10% difference)","Slightly more (10% difference)","Many fewer (35% difference)","Many more (35% difference)"],"sources":["https://www.pewforum.org/2019/01/31/religions-relationship-to-happiness-civic-engagement-and-health-around-the-world/"]}
{"question":"In the US in 2015, there were ___ abortions per 1000 live births.","category":"DFB","answers":["12","80","124","188"],"sources":["https://www.cdc.gov/mmwr/volumes/67/ss/ss6713a1.htm?s_cid=ss6713a1_w"]}'''

我想将这个字符串转换成一个python字典,其键为“question”、“category”、“answer”和“sources”。Questioncategory将始终是纯文本,而answerssources将采用带括号的列表格式。你知道吗

我假设它将需要使用正则表达式,如this answer中的形式dictionary = dict(re.findall(r"\{(\S+)\s+\{*(.*?)\}+",text)),但不能完全使它匹配我需要的所有键。你知道吗

有什么想法吗?你知道吗

确定的“重复”链接不能解决我的问题。我在使用dictionary = ast.literal_eval(text)时遇到“invalid syntax”错误,因为我没有成功地从字符串中划分出所有单独的字典。你知道吗


Tags: the字符串textinhttpsorgwwwanswers
3条回答

你可以试试这个,希望对你有帮助。我在这里还了一份清单,但这取决于你的目的。你知道吗

a = text.strip().split("\n")
import ast
b = []
for i in a:
    d = dict(ast.literal_eval(i))
    b.append(d)
>>>b
[{'question': 'In 2017, what was the approximate number of clinics in the US that provided abortion services?', 'category': 'RFB', 'answers': ['80', '800', '8000', '80000'], 'sources': ['https://www.guttmacher.org/fact-sheet/induced-abortion-united-states']}, {'question': 'Compared to actively religious US adults, how many unaffiliated US adults were active in non-religious voluntary organizations, such as charities?', 'category': 'DFB', 'answers': ['Slightly fewer (10% difference)', 'Slightly more (10% difference)', 'Many fewer (35% difference)', 'Many more (35% difference)'], 'sources': ['https://www.pewforum.org/2019/01/31/religions-relationship-to-happiness-civic-engagement-and-health-around-the-world/']}, {'question': 'In the US in 2015, there were ___ abortions per 1000 live births.', 'category': 'DFB', 'answers': ['12', '80', '124', '188'], 'sources': ['https://www.cdc.gov/mmwr/volumes/67/ss/ss6713a1.htm?s_cid=ss6713a1_w']}]

如果您完全可以保证数据源是安全的,那么它可以简单到:

exec(f"l={text}")
print(l) #{'question': 'In 2017, what was the approximate number of clinics in the US that provided abortion services?', 'category': 'RFB', 'answers': ['80', '800', '8000', '80000'], 'sources': ['https://www.guttmacher.org/fact-sheet/induced-abortion-united-states']}

如果有一个阴影的机会,恶意演员可以得到你的输入文本,然后不要这样做,但它是简单的,因为它得到。你知道吗

这个管用!!你知道吗

此代码的输出为

{'question':'abc', 'category':'abc', 'answers':['a', 'b', 'c'], 'sources': ['a', 'b', 'c']}
import json
text = '''{"question":"In 2017, what was the approximate number of clinics in the US that provided abortion services?","category":"RFB","answers":["80","800","8000","80000"],"sources":["https://www.guttmacher.org/fact-sheet/induced-abortion-united-states"]}
{"question":"Compared to actively religious US adults, how many unaffiliated US adults were active in non-religious voluntary organizations, such as charities?","category":"DFB","answers":["Slightly fewer (10 difference)","Slightly more (10 difference)","Many fewer (35 difference)","Many more (35 difference)"],"sources":["https://www.pewforum.org/2019/01/31/religions-relationship-to-happiness-civic-engagement-and-health-around-the-world/"]}
{"question":"In the US in 2015, there were ___ abortions per 1000 live births.","category":"DFB","answers":["12","80","124","188"],"sources":["https://www.cdc.gov/mmwr/volumes/67/ss/ss6713a1.htm?s_cid=ss6713a1_w"]}'''

text = '''{"question":"a","category":"a","answers":["a", "b"],"sources":["a"]}
{"question":"b","category":"b","answers":["b", "c"],"sources":["b"]}
{"question":"c","category":"c","answers":["c", "d"],"sources":["c"]}'''
outputDict = {"question":"", "category":"", "answers":[], "sources":[]}
for i in text.split('\n'):
    a = (json.loads(i))
    outputDict["question"]+=a["question"]
    outputDict["category"]+=a["category"]
    outputDict["answers"].append(a["answers"][0])
    outputDict["sources"].append(a["sources"][0])

print(outputDict)

相关问题 更多 >