假设我有一个字符串,看起来是这样的:
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”。Question
和category
将始终是纯文本,而answers
和sources
将采用带括号的列表格式。你知道吗
我假设它将需要使用正则表达式,如this answer中的形式dictionary = dict(re.findall(r"\{(\S+)\s+\{*(.*?)\}+",text))
,但不能完全使它匹配我需要的所有键。你知道吗
有什么想法吗?你知道吗
确定的“重复”链接不能解决我的问题。我在使用dictionary = ast.literal_eval(text)
时遇到“invalid syntax”错误,因为我没有成功地从字符串中划分出所有单独的字典。你知道吗
你可以试试这个,希望对你有帮助。我在这里还了一份清单,但这取决于你的目的。你知道吗
如果您完全可以保证数据源是安全的,那么它可以简单到:
如果有一个阴影的机会,恶意演员可以得到你的输入文本,然后不要这样做,但它是简单的,因为它得到。你知道吗
这个管用!!你知道吗
此代码的输出为
相关问题 更多 >
编程相关推荐