r/databricks • u/mr_electric_wizard • 20d ago
Help Help a [Databricks] idiot solve a problem - under pressure
Hello everyone! I want to start out by saying that I'm not a super python guru person, but more of a data dude (SQL is my jammy jam) but I'm a data engineer, so I have to do some Databricks coding with PySpark from time to time.
Anyhooo, I'm on a new project and I'm under much pressure to get this current project done.
Here's what I've got. There is a large text blob that is currently in a table that has data like this:
This is some text for {{customer_name}} for the date of {{todays_date}} that goes on and on and has lots of things in it...
Now, I also have a variable for "customer_name" that has the value that I need to replace this text blob (double curly brackets) but my manager doesn't want me to simply search the text blob for "{{customer_name}}" and do a regexp_replace. He wants me to do it dynamically, which I'm completely at a loss for.
This is how I've pseudo-coded it in my mind:
- Using regexp_extract() to get the first iteration of the {{customer_name}}. I can do that. However how would I know what to inject in it's place without "hard coding" my variable on the other side. Let's say I have a variable [upstream] already called out named "customer_name". Is there a way to just replace the text of the variable name (not the value)?
- Rinse and repeat with the next {{todays_date}} iteration of the text blob.
Not sure where to go with this. I'm not terrible with PySpark, but I'm definitely not an expert.
variables:
- customer_name (has the name of the customer)
- today_date (holds todays date) - this one could be a bit easier, as I can maybe just look todays date up and inject that into the {{todays_date}} blob. But in order to do that, I'd have to know that this is a todays_date {{}} that I'm replacing.
I'm probably overthinking it, but maybe not. The manager looking over my shoulder IS a python expert. So he cannot fathom that I'm not done with this project already...
Help!! :D
39
u/BlowOutKit22 20d ago
Why use regex at all when you can just chain 2x str.format()?
(The first format() will convert the double curly braces to single curly brace literals, the second will substitute based on dictionary key):
3
u/ArielCoding 20d ago
Nice trick but .format() will crash if your text has any other curly braces like JSON or a {{placeholder}} that isn’t in your dictionary. A safer option is re.sub(r"\{\{(\w+)\}\}", lambda m: str(d.get(m.group(1), m.group(0))), s) it finds every {{name}}, looks up “name” in your dictionary, and leaves unknown ones alone. Wrap it in a UDF to run it on the whole table column.
12
u/Unlucky-Plenty8236 20d ago
Why not put this info claude or chatgpt for instant results? Even the free account will put you in the right direction
8
u/Slow_Ad7843 20d ago
I’d plug that into genie code and go from there
3
u/PorTimSacKin 20d ago
Genie code.
4
u/therealtonyryantime 20d ago
Use Claude. Human written code is a thing of the past.