[英]Is there an easy way to recreate string with variables concatenated by `+` to python f-string?
我目前正在重構寫得不好的遺留代碼,代碼片段如下:
sql_tmp = (
func_out
+ "(case when "
+ self.time_name
+ ">="
+ str(int(aggFrom))
+ " and "
+ segm_var
+ " = '"
+ segment
+ "'"
+ " then "
+ colName
+ " end)"
)
或者
print(
"Variable",
newColName,
"fill share <",
str(self.min_fill_share),
"(",
self.nr_columns_done_print,
"/",
self.nr_columns_total,
")",
)
我想有一種簡單的方法將這些字符串重新格式化為 Python f-string。
AFAIK PyCharm 有一種方法可以將"".format()
類型的字符串重"".format()
f 字符串,但它不適用於加號。
有沒有簡單的方法來實現這一目標?
我決定編寫自己的腳本,並將代碼片段粘貼到長字符串中。
"".join([
# creating f-string variable, if the string has no " on side of it
"{" + stripped + "}"
if '"' not in stripped[0]
# removing "
else stripped.replace('"', "")
for stripped in [
# removing whitespace and dividing the string by '+'
x.strip() for x in " ".join(code_snippet.split()).split("+")
]
])
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.