I would personally do something like this:
import json
import datetime
today = datetime.date.today()
tomorrow = today + datetime.timedelta(days = 1)
data = [{
"date": str(tomorrow),
"price": {
"amount": 15100
}
}]
print(json.dumps(data))
Of course, after this, you can do anything you want with json.dumps(data): in your case, send it in a request.
I'm stumped on how to fix this.
import json
import os
awx_base_url = "https://127.0.0.1”
inventory_name = "VCenter - POC”
build_number = 15
deploy_frontend = 'true'
to_deploy_dict = [
{
"name": " TEST”,
"cmd": f"awx job_templates launch --inventory '{inventory_name}' --extra_vars '{{ build_number: {build_number}, deploy_frontend: {deploy_frontend} }}' 14",
},
]
for item in to_deploy_dict:
print("Executing: " + item["cmd"])
if "workflow" in item["cmd"]:
jobid = json.loads(os.popen(item["cmd"]).read())["id"]The cli returns this error: {'variables_needed_to_start': ["Value True for 'deploy_frontend' expected to be one of ['true', 'false']."]}
Bypassing my script and using the cli directly, this command is successful.
awx job_templates launch --inventory 'VCenter - POC' --extra_vars build_number: 15, deploy_frontend: 'true', 15
What is getting lost in translation here?
you should use string.format() instead of f-strings
Still if you want to use f-strings then you should use eval like this, its unsafe
DAY="1"
MONTH="12"
df = pd.DataFrame(
[{
"path":"home/data/month={MONTH}/day={DAY}"
},
{
"path":"home/data/month={MONTH}/day={DAY}"
}
]
)
a = df['path'][0]
print(eval(f"f'{a}'"))
#home/data/month=12/day=1
Thanks to Deepak Tripathi answer, the answer is to use string format. Like this:
day="1"
month="12"
conf_path=pandas.read_json("...")
path=conf_path["path"]
data=spark.read_parquet(path.format(MONTH=month, DAY=day))
This answer was posted as an edit to the question using variable (f)-string stored in json by the OP Florida Man under CC BY-SA 4.0.
Hi All,
I'm trying to dynamically change the body of this scrapy request (Offset specifically):
body = '{"xxxxQuery":{"query":"xxxxxxx","locations":[{"country":"gb","address":"xxxx","radius":{"unit":"mi","value":20}}]},"xxxxxxRequest":{"position":[1,2,3,4,5,6,7,8,9],"placement":{"channel":"WEB","location":"JxxxxxPage","property":"xxxxxxxxx","type":"xxxxxxxx","view":"SPLIT"}},"fingerprintId":"xxxxxxxxxxxxxx","offset":0,"xxxxxSize":9,"xxxxxxxx":[]}'By using :
body = f{"xxxxQuery":{"query":"xxxxxxx","locations":[{"country":"gb","address":"xxxx","radius":{"unit":"mi","value":20}}]},"xxxxxxRequest":{"position":[1,2,3,4,5,6,7,8,9],"placement":{"channel":"WEB","location":"JxxxxxPage","property":"xxxxxxxxx","type":"xxxxxxxx","view":"SPLIT"}},"fingerprintId":"xxxxxxxxxxxxxx","offset":{value},"xxxxxSize":9,"xxxxxxxx":[]}But I get an error saying the F-String is nested too deeply.
I've even tried to use the body as a dict instead of a string, change the key's value then re-convert back to string but the site returns that it is a bad request.
Any advice on how to get around it?
You can use a conditional expression in an f-string as well:
return f"{nom} {'(%s)' % dat if dat else ''}. {tit}. {jou}. {'Pubmed: ' + pbm if pbm else ''}"
or you can simply use the and operator:
return f"{nom} {dat and '(%s)' % dat}. {tit}. {jou}. {pbm and 'Pubmed: ' + pbm}"
An easy but slightly fugly workaround is to have the formatting decorations in the string.
try:
pbm = ". Pubmed: " + art['pubmedId_s']
except (KeyError, NameError):
pbm = ""
...
print(f"{nom} ({dat}). {tit}. {jou}{pbm}")