首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python:从作为单个字符串输入的表单数据生成字典

Python:从作为单个字符串输入的表单数据生成字典
EN

Stack Overflow用户
提问于 2022-09-29 17:32:57
回答 3查看 53关注 0票数 0

在使用ASANA API时,填写的表单作为一个字符串插入到notes字段中(下面的示例输出)。我正在使用python版本3.9通过Anaconda编写一本木星笔记本

我的目标是创建一个分词,其中形式问题是关键,答案是价值。例句:{"Name":"Internal Requestor", "Name of Project": "Dummy Test project"}等,这样我就可以把它作为熊猫df来储存了。有些问题确实有多部分的答案。这些答案可以作为一个单键保存,例如:{"What teams will be involved?": "Content","SEO","Creative","Ops","Pr","Internal"}

下面的示例字符串

“名称:\n内部查询\n\n\n项目名称:\n nDummy project \\n\n营销O-团队属于哪个项目?\n增加流量\n\n项目描述:\n这是一个项目测试,以查看我们是否可以将此表单中的字段转换为数据\n\n驱动该项目的策略是什么?\n效率是游戏的名称!\\n该项目的预期影响是什么?\nBe比我们现在更高效\n请将预期影响的大小排序(H,M,,)(L):\n项目目标是什么?:\n更有效率的\n更有创意的方向\n更有创意的方向\n你有多大的信心来达到这些目标?:\n\n\\n请指出这个项目的规模:\n8\n\n哪些团队将是involved?:\nContent/SEO\nCreative\nOps\nPR\nInternal Comms\nSocial\nDemandGen\nOwner\nGuest\nExternal的利益相关者\n包括外部利益相关者,请注意:请指出预期参与该项目的外部利益相关者:\n\n\n哪个内容团队?:\n content \n SEO\n\n请指出内容的预期参与项目:\n\n\n请指出SEO的预期参与项目:\n\n\n哪些创意团队?:\n Design\n Copy\n\n请指出设计人员对该项目的预期参与:\n\n\n请指出预期参与该项目的内容:\n\n\n哪个Ops团队?请指出Martech对项目的预期参与:\n\n\n请指出Martech对该项目的预期参与:\n\n\n请指出PR对该项目的预期参与:\n1\n\n请指出预期参与该项目的内部委员会:\n\n请指出社会人士对该项目的预期参与:\n\n\n请指出DemandGen对该项目的预期参与:\n\n\n请指出业主对该项目的预期参与:\n\n\n请指出预期用户的预期参与:\n8\n\n请指出用户对该项目的预期参与。参与项目:\n1\n请提供项目里程碑:\n处理9/19 -9/20\n执行9/21 -9/25\n延迟9/31\n这是一个硬的或软的最后期限?:\n hard \n\n是什么驱动这个截止日期?\n\n高效\n哪个经理批准了这个请求提交?\n比利?

我尝试使用.splitlines(),但是不确定如何利用输出从它返回的列表中构造一个dict (特别是当考虑到有多个答案的问题时,上面已经描述过了)。** StackOverflow的新成员也可以在需要时包括更多的细节**

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2022-09-29 17:40:24

把问题分成几个小部分。

根据您的数据,首先用双新行分割,然后再用一条新行分割。

代码语言:javascript
复制
>>> raw = "YOUR LONG STRING"
>>> qa = {}
>>> for group in raw.split('\n\n'):
...   question, answers = group.split('\n', 1)
...   qa[question.rstrip(':')] = answers.splitlines()
... 

这给

代码语言:javascript
复制
{'How confident are you this project will meet these objectives?': ['High'],
 'If including external stakeholders, please note below': ['Res Ops'],
 'Is this a hard or soft deadline?': ['Hard'],
 'Name': ['Internal Requestor'],
 'Name of Project': ['Dummy Test Project'],
 'Please point Analytics expected involvement in projects': ['8'],
 "Please point Copy's expected involvement in the project": ['8'],
 "Please point DemandGen's expected involvement in the project": ['3'],
 "Please point Design's expected involvement in the project": ['8'],
 "Please point Guest's expected involvement in the project": ['1'],
 'Please point Internal Comms expected involvement in the project': ['5'],
 "Please point Martech's expected involvement in the project": ['8'],
 "Please point Owner's expected involvement in the project": ['8'],
 "Please point PR's expected involvement in the project": ['1'],
 "Please point SEO's expected involvement in the project": ['5'],
...
 'What is the strategy driving this project?': ['Efficiency is the name of the '
                                                'game!'],
 'What teams will be involved?': ['Content/SEO',
                                  'Creative',
                                  'Ops',
                                  'PR',
                                  'Internal Comms',
                                  'Social',
                                  'DemandGen',
                                  'Owner',
                                  'Guest',
                                  'External Stakeholders'],
...

如果你想硬编码一些东西,使单一答案不是列表,你应该自己决定,这似乎是模棱两可的。你也应该小心重复的问题,等等。

票数 0
EN

Stack Overflow用户

发布于 2022-09-29 17:40:05

如果s是您的字符串:

代码语言:javascript
复制
d = {
    entry.split("\n")[0]: ",".join(entry.split("\n")[1:])
    for entry in s.split("\n\n")
}

如果你在一个答案中有2行换行符,这是行不通的,但在这种情况下,字符串是不明确的,我看不出有什么可以绕过它。

票数 0
EN

Stack Overflow用户

发布于 2022-09-29 17:52:42

我假设您的字符串在一个文件中,所以如果不是这样的话,只需忽略第一行;

代码语言:javascript
复制
file_as_string = ''.join(open('yourfile.txt').readlines())

outdict = {}
for i in file_as_string.split('\n\n'):
    s = i.split(':\n')
    outdict[s[0]] = s[1]

结果:

代码语言:javascript
复制
{'Name': 'Internal Requestor',
 'Name of Project': 'Dummy Test Project',
 'Which Marketing O-Team does this project belong to?': 'Increase traffic',
 'Project Description': 'This is a project test to see if we can get the fields from this form into a dataframe',
 'What is the strategy driving this project?': 'Efficiency is the name of the game!',
 'What is the expected impact of the project?': 'Be more efficient than we currently are',
 'Please rank size of expected impact (H, M, L)': 'High',
 'What are the project objectives?': 'Be more efficient\nClearer creative direction\neasier to stack rank',
 'How confident are you this project will meet these objectives?': 'High',
 'Please point the size of this project': '8',
 'What teams will be involved?': 'Content/SEO\nCreative\nOps\nPR\nInternal Comms\nSocial\nDemandGen\nOwner\nGuest\nExternal Stakeholders',
 'If including external stakeholders, please note below': 'Res Ops',
 'Please point the external stakeholders expected involvement in the project': '5',
 'Which content teams?': 'Content\nSEO',
 "Please point content's expected involvement in the project": '5',
 "Please point SEO's expected involvement in the project": '5',
 'Which creative teams?': 'Design\nCopy',
 "Please point Design's expected involvement in the project": '8',
 "Please point Copy's expected involvement in the project": '8',
 'Which Ops Teams?': 'Analytics\nMartech',
 'Please point Analytics expected involvement in projects': '8',
 "Please point Martech's expected involvement in the project": '8',
 "Please point PR's expected involvement in the project": '1',
 'Please point Internal Comms expected involvement in the project': '5',
 "Please point Social's expected involvement in the project": '5',
 "Please point DemandGen's expected involvement in the project": '3',
 "Please point Owner's expected involvement in the project": '8',
 "Please point Guest's expected involvement in the project": '1',
 'Please provide project milestones': 'Scoping 9/19 - 9/20\nExecution 9/21 - 9/25\nLaunch 9/31',
 'Is this a hard or soft deadline?': 'Hard',
 'What is driving this deadline?': 'Efficiency',
 'Which manager approved this request submission?': 'Billy Bob'}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73899582

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档