Python:synatx错误:字符串格式不正确

时间:2020-04-02 06:26:43

标签: python

我写了这段感性的代码行,但是Python说存在语法错误:

plt.savefig(f"./emvdata/{file_path.replace('G:\\emvdata\\MeasurementData\\Clinical-Data-20200220-Stage-1\\output\\', '_').replace('\\', '_')}.png")

我知道“和”的用法,但仍然无法正常工作。

1 个答案:

答案 0 :(得分:5)

在f字符串的花括号内禁止反斜杠。无论选择哪种行为,都认为该行为过于混乱。将此类表达式移出f字符串更容易理解,因此他们决定继续使用。

在异常消息中对此做了相当明确的说明:

df$New <- ifelse(grepl('.*add.*onion.*|.*onion.*add.*',df$text), "found", "not found")
#Faster option without ifelse
#df$New <- c('Not found', 'found')[grepl('.*add.*onion.*|.*onion.*add.*', df$text) + 1]
df

#                          text       New
#1          I can add get onion     found
#2          we can add to onion     found
#3   I love to add oil to onion     found
#4  I may not add oils to onion     found
#5            add salt to onion     found
#6                add get onion     found
#7                          abc not found
#8                          def not found
#9                          ghi not found
#10                         jkl not found
#11               add get onion     found
#12               add get onion     found

采用括号中的巨型表达式,然后在f字符串之外进行计算。