[英]Is there an easy way to recreate string with variables concatenated by `+` to python f-string?
I am currently refactoring poorly written legacy code, with code snippets like this:我目前正在重构写得不好的遗留代码,代码片段如下:
sql_tmp = (
func_out
+ "(case when "
+ self.time_name
+ ">="
+ str(int(aggFrom))
+ " and "
+ segm_var
+ " = '"
+ segment
+ "'"
+ " then "
+ colName
+ " end)"
)
or或者
print(
"Variable",
newColName,
"fill share <",
str(self.min_fill_share),
"(",
self.nr_columns_done_print,
"/",
self.nr_columns_total,
")",
)
I would like to have an easy way to reformat these strings into Python f-string.我想有一种简单的方法将这些字符串重新格式化为 Python f-string。
AFAIK PyCharm has a way to redo "".format()
type of strings into f-strings, but it does not work with pluses. AFAIK PyCharm 有一种方法可以将
"".format()
类型的字符串重"".format()
f 字符串,但它不适用于加号。
Is there an easy way to achieve this?有没有简单的方法来实现这一目标?
I decided to write my own script, and I paste the code snippet into long string.我决定编写自己的脚本,并将代码片段粘贴到长字符串中。
"".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.