Issue
I extract a string a python file, the string is like this.
string = "'Interest Rate <b>%.2f</b> of <b>%.2f</b> can not change to <b>%s</b> of new loan product <b>%s</b>.'%(float(OldInterestRate),float(NewInterestRate),LoanProductOldDescription,LoanProductNewDescription)"
However, I want to use this to string to translate with Flask-Babel, the supported string with variable to support with Flask-Babel has to be as this:
string = "'Interest Rate <b>{OldInterestRate}</b> of <b>{NewInterestRate}</b> can not change to <b>{LoanProductOldDescription}</b> of new loan product <b>{LoanProductNewDescription}</b>.'.format(OldInterestRate=float(OldInterestRate),NewInterestRate=float(NewInterestRate),LoanProductOldDescription=LoanProductOldDescription,LoanProductNewDescription=LoanProductNewDescription)"
Because there are too files that has to change to this kind of string format that I could manually change the code one by one.
Therefore, I want to write a program that can convert that string into a format like above.
I have tried this
import re
def convert_format(variable):
# Extract all matches of the format %(Value1,Value2,...)
matches = re.findall(r"%\(([^)]+)\)", variable)
# Splitting the matches to individual placeholders
placeholders = [match.split(',') for match in matches]
placeholders = [item for sublist in placeholders for item in sublist] # Flatten the list
# Replace the complex placeholder with the simple ones
formatted_string = re.sub(r"%\(.+?\)", "%s", variable)
# Replace the %s placeholders with the new format placeholders and prepare format() part
format_part = []
for placeholder in placeholders:
formatted_string = formatted_string.replace("%s", "{" + placeholder + "}", 1)
format_part.append(f"{placeholder}={placeholder}")
# Add the format() part to the string
formatted_string += ".format(" + ", ".join(format_part) + ")"
if '%s' in formatted_string:
formatted_string = formatted_string.replace('%s','')
return formatted_string
variable = "'Interest Rate <b>%.2f</b> of <b>%.2f</b> can not change to <b>%s</b> of new loan product <b>%s</b>.'%(float(OldInterestRate),float(NewInterestRate),LoanProductOldDescription,LoanProductNewDescription)"
output_string = convert_format(variable)
print(output_string)
But the output result was not really correct as I wanted. Please kindly help, thanks.
Solution
Try:
import re
def get_variable_name(s):
return re.sub(r"float\((.*?)\)", r"\g<1>", s)
string = "'Interest Rate <b>%.2f</b> of <b>%.2f</b> can not change to <b>%s</b> of new loan product <b>%s</b>.'%(float(OldInterestRate),float(NewInterestRate),LoanProductOldDescription,LoanProductNewDescription)"
part1, part2 = string.rsplit("%", maxsplit=1)
part2 = part2.strip(" )(")
variables = re.findall(r"((?:float\()?[^,]+\)?)", part2)
variable_names = [get_variable_name(v) for v in variables]
part1 = re.sub(r"%[^< ]+", lambda _, i=iter(variable_names): "{" + next(i) + "}", part1)
part2 = (
".format(" + ",".join(f"{n}={v}" for n, v in zip(variable_names, variables)) + ")"
)
print(part1 + part2)
Prints:
'Interest Rate <b>{OldInterestRate}</b> of <b>{NewInterestRate}</b> can not change to <b>{LoanProductOldDescription}</b> of new loan product <b>{LoanProductNewDescription}</b>.'.format(OldInterestRate=float(OldInterestRate),NewInterestRate=float(NewInterestRate),LoanProductOldDescription=LoanProductOldDescription,LoanProductNewDescription=LoanProductNewDescription)
Answered By - Andrej Kesely
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.