如何在Windows的jupyter-notebook中应用输入的语法(python3)?

我有在PyCharm上运行的这段代码,没有任何问题:

dia = input()
with open(f"FSC_{dia}.log",mode="r") as file,open(f"FSC_{dia}_saltopag.txt",mode="w") as wFile:
     for (...)

我正在尝试使其适应jupyter-notebook-py3,但出现此语法错误:

如何在Windows的jupyter-notebook中应用输入的语法(python3)?

我在stackoverflow中发现,我应该在路径前键入r而不是f来表示它是原始字符串,但是当我将其设置为原始字符串时,它无法识别输入功能:

FileNotFoundError                         Traceback (most recent call last)
<ipython-input-21-f4b2926639f7> in <module>
----> 1 with open(r"C:\Users\14122\PycharmProjects\LOGS_FSC\FSC_**{dia}**.log",mode='r') as file:
      2     counter = 0
      3     for f in file:
      4         counter = counter + 1
      5     print(counter)

FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Users\\14122\\PycharmProjects\\LOGS_FSC\\**FSC_{dia}.log**'

我认为这与我设置此原始字符串的方式有关,有人知道吗?

谢谢!

fangchufeng 回答:如何在Windows的jupyter-notebook中应用输入的语法(python3)?

您可以在字符串之前使用fr作为参数来组合它们的效果。

with open(fr"C:\<etc>\FSC_{dial}.log",mode="r") as file:
,

您是指字符串格式吗? 看这里: https://docs.python.org/3.4/library/string.html#format-examples

此示例特别说明了命名占位符的使用:

    'Coordinates: {latitude},{longitude}'.format(latitude='37.24N',longitude='-115.81W'
本文链接:https://www.f2er.com/3166762.html

大家都在问