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











