When working with the ASANA API, a form that is filled out is inserted as a single string under a notes field (sample output below). I am working in a Jupyter Notebook via Anaconda using python version 3.9
My goal would be to create a dict where the form question is the KEY and the answer(s) is the VALUE. ex: {"Name":"Internal Requestor", "Name of Project": "Dummy Test project"}
etc so that I can ultimately store it as a pandas df. Some of the questions do have multi-part answers. Those answers can be kept as a single key, example: {"What teams will be involved?": "Content","SEO","Creative","Ops","Pr","Internal"}
EXAMPLE STRING BELOW
"Name:nInternal RequestornnName of Project:nDummy Test ProjectnnWhich Marketing O-Team does this project belong to?:nIncrease trafficnnProject Description:nThis is a project test to see if we can get the fields from this form into a dataframennWhat is the strategy driving this project?:nEfficiency is the name of the game!nnWhat is the expected impact of the project?:nBe more efficient than we currently arennPlease rank size of expected impact (H, M, L):nHighnnWhat are the project objectives?:nBe more efficientnClearer creative directionneasier to stack ranknnHow confident are you this project will meet these objectives?:nHighnnPlease point the size of this project:n8nnWhat teams will be involved?:nContent/SEOnCreativenOpsnPRnInternal CommsnSocialnDemandGennOwnernGuestnExternal StakeholdersnnIf including external stakeholders, please note below:nRes OpsnnPlease point the external stakeholders expected involvement in the project:n5nnWhich content teams?:nContentnSEOnnPlease point content’s expected involvement in the project:n5nnPlease point SEO’s expected involvement in the project:n5nnWhich creative teams?:nDesignnCopynnPlease point Design’s expected involvement in the project:n8nnPlease point Copy’s expected involvement in the project:n8nnWhich Ops Teams?:nAnalyticsnMartechnnPlease point Analytics expected involvement in projects:n8nnPlease point Martech’s expected involvement in the project:n8nnPlease point PR’s expected involvement in the project:n1nnPlease point Internal Comms expected involvement in the project:n5nnPlease point Social’s expected involvement in the project:n5nnPlease point DemandGen’s expected involvement in the project:n3nnPlease point Owner’s expected involvement in the project:n8nnPlease point Guest’s expected involvement in the project:n1nnPlease provide project milestones:nScoping 9/19 – 9/20nExecution 9/21 – 9/25nLaunch 9/31nnIs this a hard or soft deadline?:nHardnnWhat is driving this deadline?:nEfficiencynnWhich manager approved this request submission?:nBilly Bobn
I attempted to use .splitlines(), but was not sure how to utilize the output to construct a dict from the list it returned (particularly when accounting for the questions that have multiple answers, described above). ** New to StackOverflow as well, can include more details as needed **
3
Answers
If
s
is your string :If you ever have 2 newlines in a row in an answer this won’t work, but in that case the string is ambiguous and I don’t see what could get around that.
Break the problem up into smaller pieces.
Based on your data, start by splitting by double new lines, then by a single new line.
which gives
if you wanted to hardcode something so that single answers weren’t lists, you should decide that yourself, that seems ambiguous.
You should also be careful of duplicate questions, etc.
I assume you have your string in a file, so if this is not the case just ignore the first row;
Results: