首页 > 解决方案 > Why is r' still duplicating the forward slashes in my code?

问题描述

So I have tried to read the solutions to Python duplicating a forwardslash from my code so it can find the file and most of the questions seem to indicate adding r' solves the problem.

In most of my code this works. But for this file path it is still duplicating all of the forwardslashes. Does anyone know why this would be the case?

I also tried using pathlib.Path to string together my path and it has produced the same result

For privacy I have removed the true file path but it is still replicating the issue. This is in my Jupyter Notebook.

additional backslash

标签: python

解决方案


“原始字符串”与常规字符串的类型完全相同,只是输入方式不同。因为它们在内存中的表示是相同的,所以它们的“原始”不会通过解析器并改变它们以后的行为方式。

因此,它们在repr()ed 时仍然以与任何其他字符串相同的方式打印:您会注意到该表示不包括r'...'印记,而只是'...'. 由于表示r'\'为非原始字符串的方式是'\\',所以解释器这样做是正确的。


推荐阅读