I am trying to return 3 values (State, Size, Department). You will see in the below code it works if the state is 1 word "Pennsylvania" but not if it is 2 words "New York". You can comment out New York and un-comment PA to see it work. I tried to use an if-else with a string split.
import json
def lambda_handler(event, context):
print('## EVENT')
print(event)
#salesoutput=event['Details']['ContactData']['Attributes'].get('salesoutput',"Not Available")
salesoutput= "New York 15 Nuts"
#salesoutput= "Pennsylvania 15 Nuts"
if len(salesoutput.split()) > 3:
prestate1 = salesoutput.split(' ')[0]
prestate2 = salesoutput.split(' ')[1]
State = prestate1 " " prestate2
Size = salesoutput.split(' ')[2]
Department = salesoutput.split(' ')[3]
# print("State:",State,"\nDistrict Size:",Size,"\nDepartment:",Department)
else:
State = salesoutput.split(' ')[0]
Size = salesoutput.split(' ')[1]
Department = salesoutput.split(' ')[2]
# print("State:",State,"\nDistrict Size:",Size,"\nDepartment:",Department)
def describe(Response):
return State(Response), District(Response), Size(Response)
CodePudding user response:
If you're reasonably certain of the input format, why not just use regular expressions?
import re
def lambda_handler(event, context):
salesoutput= "New York 15 Nuts"
pattern = re.compile(r'([\w\s] )\s(\d{1,})\s([\w\s] )')
result = pattern.search(salesoutput)
State, Size, Department = result.groups()
print("State:",State,"\nDistrict Size:",Size,"\nDepartment:",Department)
# TODO implement
return {
"State":State,
"Size":Size,
"Department": Department
}
CodePudding user response:
s1 = "New York 15 Nuts"
s2 = "Pennsylvania 15 Nuts"
for s in [s1, s2]:
*State, Size, Department = s.split(" ")
print("State:"," ".join(State),"\nDistrict Size:",Size,"\nDepartment:",Department)
CodePudding user response:
Looks like you can use rsplit
with maxsplit
:
>>> s = "New York 15 Nuts"
>>> s.rsplit(maxsplit=2)
['New York', '15', 'Nuts']
>>> s = "Pennsylvania 15 Nuts"
>>> s.rsplit(maxsplit=2)
['Pennsylvania', '15', 'Nuts']
CodePudding user response:
If the department is always one word, you can determine what to do depending on the length of the split array.
This solution is a quick fix.
The more optimal solution would be to separate the data by commas (if you have access to that data), and use salesoutput.split(',').
Note: I would split the array only once so that you are not creating multiple copies of the same split array, to save time and space.
import json
def lambda_handler(event, context):
print('## EVENT')
print(event)
#salesoutput=event['Details']['ContactData']['Attributes'].get('salesoutput',"Not Available")
salesoutput= "New York 15 Nuts"
#salesoutput= "Pennsylvania 15 Nuts"
sales = salesoutput.split(' ')
if len(sales) == 3:
State = sales[0]
Size = sales[1]
Department = sales[2]
elif len(sales) == 4:
State = sales[0] " " sales[1]
Size = sales[2]
Department = sales[3]
print("State:",State,"\nDistrict Size:",Size,"\nDepartment:",Department)
# TODO implement
return {
"State":State,
"Size":Size,
"Department": Department
#"Email":response["Item"]["Email"]
#'statusCode': 200,
#'body': json.dumps('Hello from Lambda!')
}
CodePudding user response:
@Repl.it
Thank You That did it just had to adjust for json input. This just shows my inexperience with Python...
import json
import re
def lambda_handler(event, context):
salesoutput=event['Details']['ContactData']['Attributes'].get('salesoutput',"Not Available")
pattern = re.compile(r'([\w\s] )\s(\d{1,})\s([\w\s] )')
result = pattern.search(salesoutput)
State, Size, Department = result.groups()
print("State:",State,"\nDistrict Size:",Size,"\nDepartment:",Department)
# TODO implement
return {
"State":State,
"Size":Size,
"Department": Department
}