I have a problem with string formatting in Python.
I’m using the Template object from the string class and I need it to create a template fro an RTF Hyperlink.
# the format is field{*fldinst{HYPERLINK "url"}}{fldrslt{displayed text}}, where displayed test is the old text in the column Status
rtf_template = Template(r'{field{*fldinst HYPERLINK $quote$url$quote}{fldrslt $text}}')
def format_status(row):
# row['HasResults'] is a numpy bool, so I need to convert it to a python bool
if bool(row['HasResults']):
t = rtf_template.substitute(url=row['Results Link'], text=row['Status'], quote='"')
return t
else:
return row['Status']
# 2nd step: apply the function to the dataframe. The function takes a row as input and returns a string for the column Status
clinical_trial_df['Status'] = clinical_trial_df.apply(format_status, axis=1)
Basically if in the Template I use “$url” the result will look like this, with four times the ‘”‘:
"{field{*fldinst HYPERLINK ""https://clinicaltrials.gov/ct2/show/results/NCT00463580?term=NCT00463580&draw=2&rank=1&view=results""}{fldrslt Completed}}"
If I use $url in the Template the result looks like this (without any ‘”‘ quote):
"{field{*fldinst HYPERLINK https://clinicaltrials.gov/ct2/show/results/NCT00463580?term=NCT00463580&draw=2&rank=1&view=results}{fldrslt Completed}}"
Thanks for your help