Hey everyone, I’m having trouble with my Zapier Python code. I’m trying to reformat data from Acuity Scheduling to update it in Salesforce. Here’s what I’ve got:
if "'" in input_data['Height']:
result = {'Ft': Height.split("'")[0], 'In': Height.split("'")[1]}
else:
result = {'Ft': Height, 'In': Inches}
But it’s not working as expected. I updated it to handle commas too:
if "'" in input_data['Height']:
result = {'Ft': input_data['Height'].split("'")[0], 'In': input_data['Height'].split("'")[1]}
elif "," in input_data['Height']:
result = {'Ft': input_data['Height'].split(",")[0], 'In': input_data['Height'].split(",")[1]}
else:
result = {'Ft': input_data['Height'], 'In': input_data['Inches']}
Now I’m getting a KeyError: ‘Height’. Any ideas what I’m doing wrong? I can’t seem to access the output in later steps. Thanks for any help!
hey john, i’ve had similar probs. have u tried using .get() method? it’s safer:
height = input_data.get(‘Height’, ‘’)
if “'” in height:
ft, inch = height.split(“'”)
elif ‘,’ in height:
ft, inch = height.split(‘,’)
else:
ft, inch = height, input_data.get(‘Inches’, ‘’)
result = {‘Ft’: ft.strip(), ‘In’: inch.strip()}
this should handle missing keys better. good luck!
The KeyError indicates that the key ‘Height’ isn’t present in your input_data which might be due to a misspelling or a difference in case. It’s a good idea to first print input_data to inspect its keys and the data structure. You can use:
print(input_data)
Once you’ve confirmed the key names, consider using the .get() method so your code can handle a missing key gracefully. For example:
height = input_data.get('Height', '')
if "'" in height:
result = {'Ft': height.split("'")[0], 'In': height.split("'")[1]}
elif "," in height:
result = {'Ft': height.split(",")[0], 'In': height.split(",")[1]}
else:
result = {'Ft': height, 'In': input_data.get('Inches', '')}
This will help avoid errors if the expected key is missing. Also, ensure that the incoming data from Acuity Scheduling has exactly the same field names as your code expects.
I’ve encountered similar issues with Zapier Python code before. One thing that helped me was using the ‘safe_get’ function from Zapier’s utilities. It’s a lifesaver when dealing with potentially missing keys. Here’s how you could modify your code:
from zapier_helpers import safe_get
height = safe_get(input_data, 'Height', '')
inches = safe_get(input_data, 'Inches', '')
if "'" in height:
ft, inch = height.split("'")
elif ',' in height:
ft, inch = height.split(',')
else:
ft, inch = height, inches
result = {'Ft': ft.strip(), 'In': inch.strip()}
This approach handles missing keys gracefully and simplifies your logic. Remember to test thoroughly with various input scenarios to ensure it works as expected in your Zap.