【发布时间】:2021-11-24 01:46:23
【问题描述】:
我正在使用下面的代码来生成一个可以在我的代码中使用的变量:
year_current is 21的值
代码:
month_1 = "Jan '"
month_2 = "Dec '"
'"' + month_1 + str(year_current) + '":"' +month_2 + str(year_current) + '"'
以上代码生成的输出为:
"Jan \'21":"Dec \'21"
我不想在输出中出现 "" 符号。可以做些什么来避免这种情况?
预期输出:
"Jan '21":"Dec '21" (with all the quotes intact so that I can use this in df.loc)
我也使用了以下方法:
代码:
month_1 = "'Jan '"
month_2 = ":'Dec '"
month_1 +str(year_current)+"'" +month_2 +str(year_current)+"'"
这给了我输出:
'Jan '21':'Dec '21'
但是它会在代码中对单引号造成混淆。因此这对我不起作用。
不确定是不是因为我的代码在 Jupyter 中,但解决方案没有按预期工作:
【问题讨论】:
-
@Downvote:为什么要投反对票?请解释一下,以便我将来改进。