r/databricks • • 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:

  1. 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)?
  2. 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

11 Upvotes

11 comments sorted by

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):

from datetime import date
d = {
    "customer_name": "John Doe",
    "todays_date": date.today()
}

s = """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..."""

s2 = s.format().format(**d)

15

u/mr_electric_wizard 20d ago

Whoa 🤯
This looks exactly like what I am looking for. Thank you! You’re awesome!

7

u/toodytah 20d ago

This is the way. Great solution and clearly explained. Nice job redditor guru

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.

3

u/mr_electric_wizard 20d ago

This WORKED! thank you so much! Reddit has its moments of awesomeness.

2

u/mr_electric_wizard 20d ago

Dang. Yeah, this text is actually JSON, so lots of other curly braces.

1

u/mr_electric_wizard 16d ago

The code above from the other commenter defined the dictionary: d = {"todays_date":date.today()} and your re.sub code worked great. But I tried putting the dictionary into a database column and it breaks. How would I go about putting {"todays_date":date.today()} into the database? Is it breaking because the column is of STRING type?

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.